How to deduct chances in a game I created on Xcode for iPad - 2d-games

The subject of this game is the player must avoid the cans falling from the top. I want to
give the player 3 chances, this is my code:
_chancesCount = 0;
if (CGRectIntersectsRect(_cola.frame, _cabbie.frame)) {
_chancesCount += 1;
}
if (CGRectIntersectsRect(_coffee.frame, _cabbie.frame)) {
_chancesCount += 1;
}
if (CGRectIntersectsRect(_tea.frame, _cabbie.frame)) {
_chancesCount += 1;
}
if (CGRectIntersectsRect(_soda.frame, _cabbie.frame)) {
_chancesCount += 1;
}
if (CGRectIntersectsRect(_beer.frame, _cabbie.frame)) {
_chancesCount += 1;
}
if (_chancesCount == 3) {
_currentState = _STATE_GAMEOVER;
}
I setup a label to display how many chance the player have left
but it seems that whenever the can hit and leaves _cabbie.frame(a yellow creature).
chanceCounts automatically turns back into 0. so there must be 3 cans hitting cabbie at the same time to make the game over.

Related

QProgressbar does not respond to large values

I want to build an application that calculates large computations using an idle timer. The progress bar works fine for small numbers but if I enter large numbers as input the progress bar does not display anything.
Code, where I set the QProgressbar, is as shown below.
void MainWindow::on_startButton_clicked()
{
numberEntered = ui->inputValue->text().toInt();
QIntValidator * validator = new QIntValidator(this);
ui->inputValue->setValidator(validator);
ui->progressBar->setMaximum(numberEntered);
globalIndex = 0;
timer->start(0);
ui->resultArea->append(QString("STARTED"));
}
and in another place
void MainWindow::countManager()
{
if(globalIndex == 0){
ui->resultArea->append(QString("%1 : is a Prime number").arg(globalIndex));
ui->progressBar->setValue(globalIndex);
}
if(globalIndex <= numberEntered && globalIndex != 0){
int result = primeNumberCalculator(globalIndex);
ui->progressBar->setValue(globalIndex);
if(result !=0){
ui->resultArea->append(QString("%1 : is a Prime number").arg(result));
}
}
if(numberEntered == globalIndex){
timer->stop();
ui->resultArea->append(QString("End of count!!"));
}
globalIndex++;
}

How to make jump animation in Game Maker?

for some reason when I run the game the sprite is in its jump animation and not its idle at the start even when moving left and right the sprite is still the jump animation. i followed a tutorial on youtube i know that the right and left animations work any help would be appreciated thanks.
///platform "physics"
var rkey = keyboard_check(vk_right);
var lkey = keyboard_check(vk_left);
var jkey = keyboard_check_pressed(vk_up);
//Check for ground
if (place_meeting(x, y+1, obj_solid))
{
airjump = 1;
vspd = 0;
//Jumping
if (jkey)
{
vspd = -jspd;
}
}
else
{
//Gravity
if (vspd < 10 )
{
vspd += grav;
}
//Check For airjump
if(airjump > 0)
{
if(jkey)
{
vspd = -jspd;
airjump -= 1;
}
}
}
//Moving Right
if(rkey)
{
hspd = spd;
//Left Wall-Jump
if(place_meeting(x-1, y, obj_solid) && !place_meeting(x, y+1, obj_solid)
&& !lkey)
{
vspd = -jspd;
}
}
//Moving Left
if(lkey)
{
hspd = -spd;
//Right Wall-Jump
if(place_meeting(x+1, y, obj_solid) && !place_meeting(x, y+1, obj_solid)
&& !rkey)
{
vspd = -jspd;
}
}
//Check for not moving
if((!rkey && !lkey) || (rkey & lkey))
{
hspd = 0 ;
}
//Horizontal Collisions
if(place_meeting(x + hspd, y, obj_solid))
{
while(!place_meeting(x+sign(hspd), y,obj_solid))
{
x += sign(hspd);
}
hspd = 0;
}
//Move Horizontally
x += hspd;
//Vertical Collisions
if(place_meeting(x, y+vspd, obj_solid))
{
while(!place_meeting(x, y+sign(vspd),obj_solid))
{
y += sign(vspd);
}
vspd = 0;
}
//Move Vertically
y += vspd;
//Control The Sprites
if(vspd != 0)
{
sprite_index = spr_player_jump;
image_speed = 1;
//use the next line if you have a falling animation as well but the
falling animation should be the second one
//image_index = y>yprevious;
}
else
{
if(hspd != 0)
{
sprite_index = spr_player_walk;
image_speed = .15;
}
else if(hspd = 0)
{
sprite_index = spr_player_stand;
}
}
if (!place_meeting(x,y+1, obj_solid))
{
sprite_index=spr_player_jump;
}
//Control the direction that the player is facing
if(hspd > 0)
{
image_xscale = 1;
}
else if (hspd < 0)
{
image_xscale = -1;
}
First, in the create event, set image_speed = 0; Then you have to play the animation once.
Then once you're in the step event, when the player jumps:
`while (image_index != image_number)
{
image_index++;
}
//Or you can replace image_number with the end
//value of your jump animation, if you also have other animations like
//walking then set image index to the start value before running the loop`
Also, a check to make sure the player is on the ground too (so pressing the space bar in the air doesn't make the animation play. Unless you want that.)

GameMaker Studio: How to ignore global left button when dealing with virtual keys?

I have the following problem:
When I press the pause button my characters jumps at the same time, the pause button being a VIRTUAL KEY and the jump button is keyboard_mouse_button_check(mb_left) .
I am trying to:
be able to press the pause button without making the character jump at the same time.
Obj_pause
Create Event
global.pause = 0;
Step Event
if keyboard_check_pressed(vk_space) and !global.death and !global.stop
global.pause = !global.pause
Obj_player
Create Event
// Initialize Variables
global.stop = 0;
previously_paused = false;
global.bigscore = 0;
global.col = true;
global.cartof = 0;
grav = 0.2;
hsp = 0;
vsp = 0;
jumpspeed = 4;
movespeed = 4;
antigrav = true;
jumps = 0;
jumpsmax = 2;
delay = 3;
alarm[3] = delay;
Step Event
if (global.pause) or (global.death) exit; // Pause
// Get the player's input
key_jump = mouse_check_button(mb_left);
// React to inputs
hsp = 0;
if vsp < 10
vsp += grav;
if place_meeting(x, y + 1, obj_wall)
jumps = jumpsmax;
// Double jump
if (key_jump) && (jumps > 0) && (!previously_paused)
{
jumps -= 1;
vsp = -jumpspeed;
previously_paused = global.pause;
}
// Fly
else if (jumps == 0)
{
key_jump = mouse_check_button(mb_left);
if key_jump
vsp -= 0.4
}
if global.col
{
// Horizontal Collision
if (place_meeting(x+hsp, y, obj_wall))
{
while (!place_meeting(x + sign(hsp), y, obj_wall))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
}
// Vertical Collision
if (place_meeting(x, y + vsp, obj_wall))
{
while (!place_meeting(x, y + sign(vsp), obj_wall))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
// Exit the game or restart
if keyboard_check(ord("P"))
game_restart();
if keyboard_check(vk_space)
alarm[1] = room_speed;
// Score
if background_hspeed[0] < 0
global.cartof += 10;
I have tried:
Check if I press the pause button don't do the jump To change key_jump to be equal 0 when I click on the pause button. When mouse is over the pause button to dezactivate the mouse ArchbishopDave Method
I am using:
GM:S 1.4
GameMaker Language (GML) or Drag and Drop (DnD)
Did you read event order? Step events will be runned before Keyboard and Mouse events (and virtual buttons too). So if you checked jump button in Step event, and only after it will be pressed virtual key, then your character always will jump.
You need change order of checks. For example, you can manually check, is pressed virtual key or not, before jump (check, is mouse inside coords of virtual key or not), something like this:
key_jump = mouse_check_button(mb_left);
if point_in_rectangle(device_mouse_x_to_gui(0), device_mouse_y_to_gui(0), virt_left, virt_top, virt_right, virt_bottom)
key_jump = false;
where virt_left, virt_top, virt_right, virt_bottom is coords of your virtual button.
There are many ways for improve it, but all depend on your code/architecture (depth of objects, creation order of instances, etc), so I can't give a finihsed perfect solution. If you will have a problem with fix, just share your project and I'll help.

Setting TabContainer Active Tab dynamically by visibility

I'm using AjaxControlToolkit TabContainer and dynamically setting TabPanel visiblility. When active tab is is hidden (dynamically) the whole Tab Control is hidden, so to avoid this I've written small method like this (setting first visible tab as Active Tab)
private void SetActiveTab()
{
if (tabControl1.Tabs[0].Visible)
{
tabControl1.ActiveTabIndex =0;
return;
}
if (tabControl1.Tabs[1].Visible)
{
tabControl1.ActiveTabIndex = 1;
return;
}
...
}
But this feels like but inefficient/ugly code for me..., Is there a better way of doing this...?
Choose any
for (int tabIndex = 0; tabIndex < TabContainer1.Tabs.Count; tabIndex++)
{
if(TabContainer1.Tabs[tabIndex].Visible)
{
TabContainer1.ActiveTabIndex = tabIndex;
break;
}
}
foreach (TabPanel tab in TabContainer1.Tabs)
{
if (tab.Visible)
{
TabContainer1.ActiveTab = tab;
break;
}
}
var firstVisibleTab = TabContainer1.Tabs.OfType<TabPanel>().FirstOrDefault(tab => tab.Visible);
if (firstVisibleTab != null)
{
TabContainer1.ActiveTab = firstVisibleTab;
}
BTW for such quetions better use Code Review site: Code Review

Flex AS3: ProgressBar doesn't move

I am a little stuck and need some advice/help.
I have a progress bar:
<mx:ProgressBar id="appProgress" mode="manual" width="300" label="{appProgressMsg}" minimum="0" maximum="100"/>
I have two listener functions, one sets the progress, and one sets the appProgressMsg:
public function incProgress(e:TEvent):void {
var p:uint = Math.floor(e.data.number / e.data.total * 100);
trace("Setting Perc." + p);
appProgress.setProgress(p, 100);
}
public function setApplicationProgressStep(e:TEvent):void {
trace("Setting step:" + e.data);
appProgressMsg = e.data;
}
I want to reuse this progress bar alot. And not necessarily for ProgressEvents, but when going through steps.
For instance, I loop over a bunch of database inserts, and want to undate the progress etc.
Here is a sample:
public function updateDatabase(result:Object):void {
var total:int = 0;
var i:int = 0;
var r:SQLResult;
trace("updateDatabase called.");
for each (var table:XML in this.queries.elements("table")) {
var key:String = table.attribute("name");
if (result[key]) {
send(TEvent.UpdateApplicationProgressStep, "Updating " + key);
i = 1;
total = result[key].length;
for each (var row:Object in result[key]) {
//now, we need to see if we already have this record.
send(TEvent.UpdateApplicationProgress, { number:i, total: total } );
r = this.query("select * from " + key + " where server_id = '" + row.id + "'");
if (r.data == null) {
//there is no entry with this id, make one.
this.query(table.insert, row);
} else {
//it exists, so let's update.
this.update(key, row);
}
i++;
}
}
}
}
Everything works fine.
That is, the listener functions are called and I get trace output like:
updateDatabase called.
Setting step:Updating project
Setting Perc 25
Setting Perc 50
Setting Perc 75
Setting Perc 100
The issue is, only the very last percent and step is shown. that is, when it's all done, the progress bar jumps to 100% and shows the last step label.
Does anyone know why this is?
Thanks in advance for any help,
Jason
The new code, which works awesomely I might add:
public function updateDatabase(result:Object, eindex:int = 0, sindex:int = 0 ):void {
var total:int = 0;
var i:int = 0;
var j:int;
var r:SQLResult;
var table:XML;
var key:String;
var elems:XMLList = this.queries.elements("table");
var startTime:int = getTimer();
var row:Object;
for (i = eindex; i < elems.length(); i++) {
table = elems[i];
key = table.attribute("name");
if (!result[key])
continue;
total = result[key].length;
send(TEvent.UpdateApplicationProgressStep, "Updating " + key);
for (j = sindex; j < result[key].length; j++) {
if (getTimer() - startTime > 100) {
setTimeout(updateDatabase, 100, result, i, j);
send(TEvent.UpdateApplicationProgress, { number:j, total: total } );
return;
}
row = result[key][j];
r = this.query("select * from " + key + " where server_id = '" + row.id + "'");
if (r.data == null) {
//there is no entry with this id, make one.
this.query(table.insert, row,false);
} else {
//it exists, so let's update.
this.update(key, row,false);
}
}
send(TEvent.UpdateApplicationProgress, { number:1, total: 1 } );
}
}
Flash is single threaded. The display will not update until your function returns. For this reason, you should never have any code that runs for longer than about 100ms (1/10th of a second), otherwise the UI (or even the entire browser) will appear to be locked up.
The general solution is to split up your work over multiple frames, here is some pseudo-code:
function doWork(arg1:Obj, arg2:Obj, start:int=0) {
var startTime = getTimer(); // store starting time
for(i=start; i<length; i++) {
if(getTimer() - startTime > 100) { // see if we've been working too long
trace("Current progress: "+(i/length * 100)+"%");
updateProgress( i / length );
setTimeout(doWork, 100, arg1, arg2, i); // schedule more work to be done
return; // stop current loop
}
trace("Working on item "+i);
// processing here
}
trace("All work done");
}
doWork(data1, data2); // start the work
Your pseudo-code works for updating the progress bar however in my case my "work" was copying of files from DVD to the appStorageDirectory which seem to reintroduce the same issue that your work around resolved - the progress bar now does not update
Here is my hack of your solution
function doWork(arg1:int, arg2:int, start:int=0) {
var startTime = getTimer(); // store starting time
for(var i:int=start; i<arg2; i++) {
if(getTimer() - startTime > 100 ) { // see if we've been working too long
trace("Current progress: "+(i/arg2 * 100)+"%");
setTimeout(doWork, 100, i, arg2, i); // schedule more work to be done
return; // stop current loop
}
trace("Working on item "+i);
dispatchEvent(new progressMadeEvent("incrementChange",i,arg2))
var dir:String = copyRes[i].nativePath.toString().split(OSSep).pop()
copyRes[i].copyTo(appStore.resolvePath(dir)) // copies dir from DVD to appStorageDir
}
trace("All work done");
}

Resources