simply, before I start new level of my game, I show countdown (using CCLabelTTF, Cocos2D).
I have this code:
centerLevelLabel.string=#"New level";
centerLevelLabel.visible=true;
[centerLevelLabel runAction:[CCFadeOut actionWithDuration:1]];
while (centerLevelLabel.numberOfRunningActions>0) {
}
centerLevelLabel.string=#"3";
while (centerLevelLabel.numberOfRunningActions>0) {
}
centerLevelLabel.string=#"2";
while (centerLevelLabel.numberOfRunningActions>0) {
}
centerLevelLabel.string=#"1";
while (centerLevelLabel.numberOfRunningActions>0) {
}
//some code bellow
I want to show thist label and then I need continue with code bellow. Problem is, that UI is freezed and in first while block is running infinite loop. But i don`t kwon why, because when running actions is finished, numberOfRunningActions is zero.
Is there any other way, how could I wait for end of running actions and then continue?
Thank you
You could serialize the action by using a CCSequence and add the CCCallFuncN to execute some code after the original action completes.
id action1 = [CCFadeOut actionWithDuration:1];
id action2 = [CCCallFuncN actionWithTarget:self selector:#selector(finishedRunning)];
id sequenceActions = [CCSequence actions:action1, action2, nil]];
[centerLevelLabel runAction:sequenceActions];
....
-(void) finishedRunning {
// Do stuff after action finishes
}
This will always be an infinite loop:
while (centerLevelLabel.numberOfRunningActions > 0)
{
}
It's similar to writing:
while (value > 0)
{
}
For the time the while loop is running, the thread is blocked. Therefore no other code will run that updates the number of actions, or that might change the value of value.
Related
Hopefully this is a fairly simple question. I create an instance variable named tutorialBox1 from my main game scene Grid.m. Inside my class TutorialBox.m, I create a sprite named textBox1, and as I tap on the main game scene, I want the position of that sprite, textBox1 to be updated. I've been trying to figure out for hours why the position of the sprite will not update after adding the child once. Here is my attempt, and all the necessary code.
Grid.m
# implementation Grid {
TutorialBox *tutorialBox1;
}
-(void)checkTutorials {
//This method is called upon loading of the scene
tutorialBox1 = (TutorialBox*)[CCBReader load:#"TutorialBox"] //creates instance of variable using Spritebuilder (don't worry if you don't use SB, this part works)
[tutorialBox1 updateBoxDisplay:1] //used to add text box child to textBox1
[self addChild:tutorialBox1];
}
-(void)gridTapped {
//When game scene is tapped, this is called to update position of textBox1 in tutorialBox1
[tutorialBox1 updateBoxDisplay:2]; //updates position of textBox1 in tutorialBox1
}
TutorialBox.m
# implementation Grid {
CCSprite9Slice *textBox1;
}
-(void)didLoadFromCCB {
//Called upon initialization of tutorialBox1
textBox1 = [CCSprite9Slice spriteWithImageNamed:#"RetroSprites/tutorialtextBox1.png"];
}
-(void)updateBoxDisplay:(int)newTutorialLevelNumber {
if (newTutorialLevelNumber == 1) {
textBox1.position = ccp(0,0);
[self addChild:textBox1];
}
else if (newTutorialLevelNumber == 2) {
textBox1.position = ccp(100,100)
}
}
TutorialBox.h
#interface TutorialBox : CCNode
- (void)updateBoxDisplay:(int)newTutorialLevelNumber
#end
I have checked to make sure everything is running properly with print statements, so it isn't an issue with anything else. It is simply being called, but the position in my main game scene, Grid.m is not being updated.
Thanks for the help!
I am not sure about this, but I guess its issue with the threading. Give a try with this code:
-(void)updateBoxDisplay:(int)newTutorialLevelNumber {
dispatch_async(dispatch_get_main_queue(), ^{
if (newTutorialLevelNumber == 1) {
textBox1.position = ccp(0,0);
[self addChild:textBox1];
}
else if (newTutorialLevelNumber == 2) {
textBox1.position = ccp(100,100)
}
});
}
Could someone please help me make this code work.
(I want to put this code in a movieclip)
on roll over a button a movieclip keeps going to the right.
on roll out of the button the movieclip stops.
if (rollOver(_root.Rbutton)) {
_x += speed;
this is what i got so far.
The button and the movieClip are on the stage of your main timeline:
var xspeed:Number = 2;
function mcToMove():Void {
mc._x += xspeed;
}
myButton.onRollOver = function():Void {
onEnterFrame = mcToMove;
}
myButton.onRollOut = function():Void {
delete onEnterFrame;
}
I would like to stop function like this:
- (void)update:(NSTimeInterval)currentTime
{
if (self.lastUpdateTimeInterval)
{
_dt1 = currentTime - _lastUpdateTimeInterval;
}
else
{
_dt1 = 0;
}
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
[self moveObject];
}
I now timers stop like: [timer1 invalidate], but how to stop function that is running on timer interval?
If you want to stop the animation you can call removeAllActions on your sprite object:
[_sprite removeAllActions];
But I think you need to also run the animation just if the sprite is not running it already. To do that you can use:
if (![_sprite hasActions])
{
[_sprite runAction:action];
}
If you want to stop just one animation (not all) you can use runAction:withKey: method when you run action and you can see if the action is running by calling actionForKey: or you can stop it by calling removeActionForKey.
I have this code:
while (!found) {
char letterGuess=(char)([[[wordList letterRank:lengthOfWord]objectAtIndex:yescount]intValue]+97);
NSString *stringLetter=[NSString stringWithFormat:#"%c",letterGuess];
if ([prevGuess count]<=0) {
alreadyGuessed=NO;
}else {
if ([prevGuess containsObject:stringLetter]) {
alreadyGuessed=YES;
yescount++;
}else{
alreadyGuessed=NO;
}
}
if (!alreadyGuessed) {
[prevGuess addObject:stringLetter];
[self drawYesNo];
NSLog(#"%c",letterGuess);
}
if (userAnswer) {
}else {
[self noGuess:letterGuess];
}
if ([wordList count]<=1) {
NSLog(#"word found");
found=YES;
}
}
basically, it takes a letter from a sorted array, checks it against another array containing all the previous letters that were entered, and if it is not in that array, it will call [self drawYesNo], which basically sets up and draws a yes and a no UIButton, and based on what the user presses, changes the 'userAnswer' variable.
My problem is that this loop executes so quickly that the objectAtIndex quickly exceeds the bounds of the array, and throws an error. I need some way to pause during the [drawYesNo] method, and allow the user to actually make a decision.
I know there are answers to similar questions on StackOverflow, but I just can't make heads or tails of them. Can someone please explain this for a very new OO programmer?
I can't really figure out all the logic of your code, but it sounds like you need to refactor it into different methods. Instead of having the code in a while loop, you should have one method that does most (all? I can't tell) of what you posted (without the while loop). You call this once when your app starts, then after the user clicks a button -- in the button's action method you change the value of userAnswer, and then call that method again. This keeps going until some condition is met in your button method that would cause it to stop.
I am trying to run an if statement in selenium that would verify if a checkbox was checked and if it was not perform a certain action. I tried
if (selenium.verifyChecked("checkbox")=false){
//perform actions
} else {
//perform different actions
};
and it said that that did not work. How would you do it?
The Selenium command isChecked returns a boolean, so you should be able to do the following:
if (selenium.isChecked("checked")) {
//perform actions
} else {
//perform different actions
};
if (selenium.verifyChecked("checkbox")=false){
Is wrong. It's assigning false to the return value of the function, which is clearly wrong.
It should be:
if (selenium.verifyChecked("checkbox") == false) {