How do I call a particular function at the end of a runAction? - function

I am performing a runAction on a sprite to move to a position.
[[crBalls[cb.count] getball] runAction:[CCSequence actions:[CCMoveTo actionWithDuration:timeToTravel position:ccp(xIntercept,yIntercept)],[CCMoveTo actionWithDuration:0.05 position:FD],nil]];
Once the sprite moves to the desired position(FD) then I want to call a function at that time. Right now I am scheduling a selector to be called after a delay of 'timeToTravel' which is the time taken by the above action to finish performing.(I am using scheduler instead of performSelector since perform selector is more prone to problems)
[self schedule:#selector(placeThatDamnBall) interval:timeToTravel+0.05];
-(void) placeThatDamnBall
{
[self unschedule:#selector(placeThatDammBall)];
[self ballPlacedIn:FD.x :FD.y :cb.type : cb.count];
}
But this is not entirely reliable and may cause problem in a rare case where the function might get called before the sprite reaches the destination. Is there any way I can avoid having to call a selector and be able to call the function once the sprite has truly reached the destination?
Thanks

Add a CCCallFunc at the end of your sequence:
[[crBalls[cb.count] getball] runAction:
[CCSequence actions:
[CCMoveTo actionWithDuration:timeToTravel position:ccp(xIntercept,yIntercept)],
[CCMoveTo actionWithDuration:0.05 position:FD],
[CCCallFunc actionWithTarget:self selector:#selector(placeThatDamnBall)],
nil]];

Related

What's code sequence in the below cocos2dx code?

I'm new to cocos2dx. In the below code, _nextProjectile is added to the children of "this" in the callback function which is executed after _nextProjectile->runaction (since "finish shoot" is always printed after "run action"). However, I print the position of _nextProjectile in finishShoot(), which is exact the same with that I set when I created the _nextProjectile.
So my question when does the _nextProjectile->runAction is executed actually?
Thank you.
{
_player->runAction(Sequence::create(RotateTo::create(rotateDuration, cocosAngle), CallFuncN::create(CC_CALLBACK_0(HelloWorld::finishShoot, this)), NULL));
// Move projectile to actual endpoint
printf("run action\n");
_nextProjectile->runAction(Sequence::create(MoveTo::create(realMoveDuration, realDest), CallFuncN::create(CC_CALLBACK_1(HelloWorld::spriteMoveFinished, this)), NULL));
}
void HelloWorld::finishShoot()
{
// Ok to add now - we've finished rotation!
printf("finish shoot\n");
this->addChild(_nextProjectile);
// Add to projectiles vector
_projectiles.pushBack(_nextProjectile);
_nextProjectile = NULL;
}
Both actions run simultaneously. The player rotates for rotateDuration before calling the callback, at the same time nextprojectile (if not null) already starts moving.
It seems like you may want to move the nextprojectile runaction in the finishShoot method instead.

How to check if a sprite has been removed

I create 10 sprites in a for loop and add them to an array:
this.mysprite = new mysprite()
this.addChild(this.mysprite)
sprite_array.push(this.mysprite)
I then remove a sprite:
shared_game_layer.removeChild(sprite_array[0], true);
I then use another for loop to perform some actions on the sprites that have not been removed, wanting to bypass the removed sprites. The following code is executed before and after the sprite has been removed however the console always logs "y"
if (sprite_array[0])
console.log("y");
else
console.log("n");
You can call the getParent function and see if the pointer to the Parent returned is nil.
if (sprite_array[0]->getParent())
console.log("y");
else
console.log("n");

in AS3, removeEventListener(Event.ENTER_FRAME) is not working

I have been dealing with this problem for days already. I am at my wits' end!
I can't seem to find a definitive answer anywhere on any of the forums, documentation, etc.
Everything looks fine at first run, or when I load a next level for the user to play. But if the user hits the ESC key to load a different level, the ENTER FRAME listener does not get removed and it duplicates all the triggers in it, showing the player going really fast, and all funky, because it builds on top of the previously instantiated ENTER FRAME listener.
I don't know if I have a problem of an anonymous function, or an unknown instance being referenced in my removeEvent... command... Bottom line, I give up and I need this working HELP!!!
Here's the code:
function initPlay():void
{
//code here determining what display object to add to the list and assign it to the currentLevel variable (a movieclip)
if(userIsLoadingOtherLevel){
removeEnterFrameListener();
addChild(currentLevel);
}
if(userIsGointToNextLevel)
addChild(currentLevel);
currentLevel.addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
//collision detection, parallax scrolling, etc, etc is done here.
if(allCoinsCollected)
loadNextLevel();
if(ESCKeyPressed)
ESCKeyPressHandler();
}
function loadNextLevel():void
{
removeChild(currentLevel);
newLevelToLoad++
removeEnterFrameListener();
initPlay();
}
function ESCKeyPressHandler():void
{
removeChild(currentLevel);
initPlay();
}
function removeEnterFrameListener();
{
currentLevel.removeEventListener(Event.ENTER_FRAME,onEnterFrame)
trace("currentLevel.hasEventListener(Event.ENTER_FRAME) = "+currentLevel.hasEventListener(Event.ENTER_FRAME)); //outputs TRUE if called from loadNextLevel but FALSE if called from initPlay() !!!
}
}
I also tried to add and remove the eventListener to stage, MovieClip(Root), or nothing at all and the result is always the same.
I know that there may be other ways to design such a process, but please note I am not really flexible at the moment on doing this because the project is very long (about 4000 lines of code) and removing the ENTER FRAME this way, crazy or not should still work!!
THANK YOU in advance for anyone willing to help.
The problem appears to be the nested functions inside the initPlay() method.
Each time you call initPlay() you are defining new functions. Some of these nested functions call initPlay() themselves.
Functions are objects (memory references). So each time you call initPlay() you are making new references to new functions. So when you try to remove an event listener, you're only able to remove one of these event handlers (the one in the current scope of execution).
I'm not sure if I'm explaining this clearly, perhaps this example will help. I'll use numbers to represent the references to each function, and a simple scenario that is similar to yours:
function example():void
{
addEventListener(MouseEvent.CLICK, mouseClickHandler);
function mouseClickHandler(event:Event):void
{
if (someCondition)
{
example();
}
else
{
removeEventListener(MouseEvent.CLICK, mouseClickHandler);
}
}
}
When we run this function the first time, a new function is defined within the scope of the example() function. Lets use the number 1 to represent the reference to this nested function. someCondition is true on the first time around, and so the example() function is called again.
On the second execution of the example() function, a new reference to the mouse event handler is created (#2). We also add the event listener again. At this point, there are two event handling functions in memory, and both will be executed when the event is dispatched.
Let's say that in the second invocation of example() that someCondition is false and now we want to remove the listener. When we call:
removeEventListener(MouseEvent.CLICK, mouseClickHandler);
It's referring to event handler #2. Event handler #1 still exists, and because it's hidden in the scope of the first invocation of example() it can't be removed here.
My simple example breaks down after this... but I hope it makes it clear why your event handlers shouldn't be nested inside a function. Admittedly, this is difficult to describe and even more so in a real world example like yours. But I'm pretty confident that this is the source of most, if not all, of the issues you describe.
Here's how I was able to get around this without changing the scope of the nested functions (although I agree that would be the preferred solution) by creating a boolean variable called "loadingNewGame" and changing it to true from outside the onEnterFrame (in fact, this assignment was done from initPlay() and then from onEnterframe I called removeEnterFrameListener() function. This did the trick.
here's the code in case anybody is interested:
// package, and other code here.
var loadingNewGame:Boolean = new Boolean(false);
function initPlay():void
{
//code here determining what display object to add to the list and assign
//it to the currentLevel variable (a movieclip)
if(userIsLoadingOtherLevel)
{
loadingNewGame = true;
removeEnterFrameListener();
addChild(currentLevel);
}
if(userIsGointToNextLevel)
addChild(currentLevel);
loadingNewGame:Boolean = false;
currentLevel.addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
if(loadingNewGame)
removeChild(currentLevel);
//collision detection, parallax scrolling, etc, etc is done here.
if(allCoinsCollected)
loadNextLevel();
if(ESCKeyPressed)
ESCKeyPressHandler();
}
function loadNextLevel():void
{
removeChild(currentLevel);
newLevelToLoad++
removeEnterFrameListener();
initPlay();
}
function ESCKeyPressHandler():void
{
initPlay();
}
function removeEnterFrameListener();
{
currentLevel.removeEventListener(Event.ENTER_FRAME,onEnterFrame)
trace("currentLevel.hasEventListener(Event.ENTER_FRAME) = "+currentLevel.hasEventListener(Event.ENTER_FRAME));
//outputs true
}

How to keep AS3 code simple

This is my original pseudo code:
function1();
function1():void{
//do something
after mouseclick do function2
}
function2():void{
//do something
after animationfinish do function3
}
etc..
Can I get it into something like this?:
function1();
after mouseclick do function2()
after animationfinish do function3()
What is an easy way to get listeners on the top level?
If I understand your question correctly, when function1 is called you want to addEventListener(MouseEvent.CLICK, function2);
Are you using Actiosncript to animate or Keyframes?
I know with TweenMax you can add a function to call at the end of the tween. If using fl.Transtions.Tween you can listen for TweenEvent.MOTION_FINISH and then call function3. I don't mess much with the timeline in Flash, since I feel more in control with coding it, but I know you can add code at the last frame of an animation to call function3.
If I am completely missing you question feel free to clarify what exactly you are seeking.
If i good understand - You like to create functions chain .
I depends what You like to do , problem is in many possibilities.
For call few functions i use this class :
https://github.com/turbosqel/as3SupportLib/blob/master/as3SupportLib/src/turbosqel/utils/CountCall.as
You can also use deeper and more elastic way and add next callbacks to function :
var func:Function = function():void { // declare new function
... function body // your class actions
for each(var call:Function in arguments.callee){ // get functions
call(); // call function
}
}
func["someFunction"] = someFunction; // add function as dynamic value
func["otherFunction"] = anotherFunctionToCall; // add another function
func(); // call function

Fake mouseclick on hover AS3

This morning I stumbled to this question:
When hovering a button, is it possible to click it automatically after X seconds? So the user doesn't need to click it with his mouse?
How can I let Flash believe my mouse really clicked some button on the stage or brought up with as3?
I have a lot of buttons in my movie. So I prefer to use some code which will cover this function for all existing or coming up buttons in my movie.
I would normally use a code like this but is there some workaround to accomplish this in a different way? I do no want to add code to every button.
this.addEventListener(MouseEvent.OVER, onMouseClickEvent);
public function onMouseClickEvent(event:Event)
{
trace(event);
if(event.buttonDown) // if button goes down normally
trace("MOUSE CLICKED NORMALLY");
else
trace("left button was not down");
}
The easiest way i think, is to subclass Button.
Then you should add mouse over/out listeners, add click listener that looks like that
:public function clickListener(event:MouseEvent = null){...}
When the mouse is hovering, raise a flag that the mouse is on the object, start a timer and when the timer callback function is called, you check the if the flag (you turn the flag down, when the mouse is out) is true and just call clickListener()
Listen for MouseEvent.MOUSE_OVER and start a timer, at the end of which the button will send the MouseEvent.CLICK event. In the mouseover handler, use the SystemManager to add a listener for MouseEvent.MOUSE_OUT which cancels the timer. The timer removes the listener using the SystemManager as well. So does clicking the button.
Finally! Solved!
This did the trick:
public function runOnce(event:TimerEvent):void {
btnSignal.dispatch("KEYBOARD", btnCode);
}
Robusto & Radoslav Georgiev: Thank you for pointing the right direction!
(I'm answering this a little late but would like to give input for future people).
One way to 'skin this cat' is to simply let your hover event trigger a timer (i.e. 3 seconds). In an EnterFrame or other function let a number or Boolean change when 3 seconds is reached.
//Pseudo code
if(timer == 3)
{ numberVar = 1;
//or
BooleanVar = True;
}
else
{
numberVar = 0;
//or
BooleanVar = false;
}
//end
Then just as you connected your methods to a mouseEvent, connect those same methods to fire when numberVar == 1 or BooleanVar == True. That's it.
For super simplicity and readability let your MouseClickEvent just be numberVar = 1 or BooleanVar = True.
These become super simple to implement over time and in my experience are 'very' error proof. Easy to fix also in the case of a typo or something else. No super elusive imports either. Hope that helped.
Great question by the way (+ 1)
:D