Pause a CCNode for a while time - cocos2d-x

I want to Pause a CCNode for a while and after that resume it again.
when I use below code separately they work but when I want to resume this CCNode for a specific time when a button clicked it dose't work and occur an Assertion failed(that is: pElement->paused == bPaused ).
//for pausing
this->pauseSchedulerAndActions();
//for resuming
this->resumeSchedulerAndActions();
I use below codes:
#define TIME_FOR_RESUME 5.0f
//function that called when my button click
void myClass::myFunc(CCObject * pSender)
{
this->pauseSchedulerAndActions();
this->scheduleOnce(schedule_selector(myClass::myResumeFunction), TIME_FOR_RESUME);
}
void myClass::myResumeFunction(float dt)
{
this->resumeSchedulerAndActions();
}

you can use this for pausing
this->unscheduleAllSelectors();
and this for resume
this->scheduleUpdate();

Related

How to repeat a Litho animation

The Litho animation examples all begin when a user triggers an event. But I need an animation that begins right away and continues indefinitely. In other words, I have the same problem as How to run a Litho animation automatically? but I need a solution for Litho animations as opposed to basic Android animations.
Note, I asked a related question How to run a Litho animation automatically? when I tried to modify one of Litho's examples to initiate the animation without a user event. But the question I'm asking now is how to repeat the animation once it's started?
To start a Litho animation automatically and repeat it indefinitely, I modified RTAnimationComponentSpec by starting a TimerTask:
#OnCreateInitialState
static void createInitialState(
ComponentContext c) {
startRepeatingAnimation(c);
}
static void startRepeatingAnimation(final ComponentContext c) {
Log.e(TAG, "Repeat animation handler: about to scheduleAtFixedRate");
TimerTask animateRepeat = new java.util.TimerTask() {
public void run() {
try {
Log.e(TAG, "Repeat animation handler: about to updateStateAsync");
RTAnimationComponent.updateStateAsync(c);
} catch (Exception e) {
Log.e(TAG, "Repeat animation handler: exception while animating: [" + e + "]");
}
}
};
new java.util.Timer().scheduleAtFixedRate(animateRepeat, 0, FADE_IN_OUT_DURATION + FADE_IN_DELAY + FADE_IN_STAGGER_DELAY);
}
private static final String TAG = "RTComponentSpec";
I'm not sure this is a valid use of createInitialState() though. According to the documentation, it's "To set an initial value for a state". By state, Litho means variables marked #State. Informally, though, the animation is part of the state and the TimerTask does need to be started. Semantically, initializing the TimerTask seems like it belongs to creating the initial state.
Empirically, the logs showed what I wanted. The initial log message, "Repeat animation handler: about to scheduleAtFixedRate" appears once followed by periodic instances of "Repeat animation handler: about to updateStateAsync".
I suppose the solution might also work with other Android mechanisms for scheduling work on a periodic basis

Libgdx Timer pause's on lost focus?

I noticed if I use GDXs Timer class on a default, newly created LibGDX project it will not fire at all while the application is minimized or not in focus.
At least this is true on Desktop deployment Windows 10.
JAVAs own timer class, meanwhile, fires regardless of focus.
A simple example app to demonstrate the difference;
#Override
public void create () {
//gdx timer (does not update when focus lost);
//------------------
com.badlogic.gdx.utils.Timer.schedule(new com.badlogic.gdx.utils.Timer.Task(){
#Override
public void run() {
long currentTime = System.currentTimeMillis();
Log.info("____GDX_____________________:"+currentTime+"_________");
}
}
, 0
, 1.0f );
//java timer (updates when focus lost);
//-----------------
Timer test = new Timer();
test.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
long currentTime = System.currentTimeMillis();
Log.info("___JAVA____________________:"+currentTime+"_________");
}
}, 0, 1000);
}
Running this you can clearly see both logs firing when in focus, and only Javas when not in focus.
My questions are;
a) Is the behavior of LibGDXs timer expected, or have I done something wrong in setup? The description of LibGDXs timer doesn't seem to mention the auto-pause.
b) I wish my application to run in the background unless explicitly paused. Merely,say, alt+tabbing should not be enough.
Should I just switch to using JAVAs timer? Does this have cross-platform implications?
Thanks,
Darkflame
a) Yes, the documentation of TimerThread, which handles Timer, says:
Manages the single timer thread. Stops thread on libgdx application
pause and dispose, starts thread on resume.
b) Since libGDX Timer is nothing special than just a Thread, which listen for application change (pause, resume etc.), with list of tasks, it should be fine to use Java's Timer. Since it is from 1.3 (and the libGDX target is 1.6) it should not have any cross-platform implications.

SImple Coloring Book: Error #1009: Cannot access a property or method of a null object reference

I am creating a simple flash coloring book and am not very familiar with as3 programming language.
I entered the following code,and when I attempted to press the back button in the test movie I got that error.
stop();
back_btn.addEventListener(MouseEvent.CLICK, GoToChooseA);
function GoToChooseA(event:MouseEvent):void
{
gotoAndStop("Choose");
}
color_scroll.mask = myMask;
var goY: Number = color_scroll.y;
stage.addEventListener(Event.ENTER_FRAME, scrollManage);
function scrollManage(Event): void {
color_scroll.y += (goY - color_scroll.y) / 20;
}
up_btn.addEventListener(MouseEvent.MOUSE_DOWN, scrollUP);
down_btn.addEventListener(MouseEvent.MOUSE_DOWN, scrollDown);
function scrollUP(MouseEvent): void {
goY += 20;
}
function scrollDown(MouseEvent): void {
goY -= 20;
}
*
It seems to indicate the error is here
color_scroll.y += (goY - color_scroll.y) / 20;
But I'm really bummed because I'm not really sure how to proceed from there.
Whenever you gotoAndStop() to a different keyframe, your current frame is invalidated and all its members destroyed. Listeners persist, if they are attached to an object that persists. So, right after you call GoToChooseA(), your color_scroll is destroyed, and then the listener attached to stage is called and tries to modify a destroyed object, there goes your 1009. The solution is either manually remove the event listeners "scrollManage", "scrollUp", "scrollDown" before you change the frame, at least "scrollManage" because it's attached to stage, or stop using frames altogether, but even then you'll have to control your event listeners.
You could add some logic to your function to check if you are in the right frame and then proceed. I am not familiar with frames so the condition would be something like this._currentframe == 2 or timeline.currentFrame == 2.
function scrollManage(Event): void {
if ( condition ) {
color_scroll.y += (goY - color_scroll.y) / 20;
}
}
If you are not at the right frame (in my example that is frame 2), the function does not execute any code.
This error means you are trying to modify something that is no longer existent.

Proceeding in Movieclip After Animation Finishes AS3

I have a Movieclip on my stage which contains two buttons: Back and Next. Also on the timeline, I have another Movieclip which contains an animation. After the next button is clicked, I'd like to have the animation transition into the next animation without jumping. So, is there any way I can listen for the animation to be finished so that I can time the transitions seamlessly?
Here is the code for my Next and Back buttons (which are movieclip buttons which is why there is all of the extra code) which just switch between frames as of now:
//NEXT BUTTON
nextBtn.buttonMode = true;
nextBtn.addEventListener(MouseEvent.ROLL_OVER, nextBtnOver);
nextBtn.addEventListener(MouseEvent.ROLL_OUT, nextBtnOut);
nextBtn.addEventListener(MouseEvent.MOUSE_DOWN, nextBtnDown);
nextBtn.addEventListener(MouseEvent.MOUSE_UP, nextBtnUp);
function nextBtnOver(e:MouseEvent):void
{
nextBtn.gotoAndPlay("over");
}
function nextBtnOut(e:MouseEvent):void
{
nextBtn.gotoAndPlay(9- (nextBtn.currentFrame-1));
}
function nextBtnDown (e:MouseEvent):void
{
nextBtn.gotoAndPlay("down");
}
function nextBtnUp (e:MouseEvent):void
{
nextBtn.gotoAndPlay(5);
MovieClip(root).nextFrame();
}
//BACK BUTTON
backBtn.buttonMode = true;
backBtn.addEventListener(MouseEvent.ROLL_OVER, backBtnOver);
backBtn.addEventListener(MouseEvent.ROLL_OUT, backBtnOut);
backBtn.addEventListener(MouseEvent.MOUSE_DOWN, backBtnDown);
backBtn.addEventListener(MouseEvent.MOUSE_UP, backBtnUp);
function backBtnOver(e:MouseEvent):void
{
backBtn.gotoAndPlay("over");
}
function backBtnOut(e:MouseEvent):void
{
backBtn.gotoAndPlay(9- (backBtn.currentFrame-1));
}
function backBtnDown (e:MouseEvent):void
{
backBtn.gotoAndPlay("down");
}
function backBtnUp (e:MouseEvent):void
{
backBtn.gotoAndPlay(5);
MovieClip(root).prevFrame();
}
Thanks, any help is appreciated.
If your animations are timelined, call an animationComplete function in the last frame of your animation movieClip. This function would live at the same scope as your posted code above, so you just have to be able to path to it properly from within the animation clip. For instance, if the code is one level outside the animation movieClip (the mc's parent) then you might call this.parent.animationComplete(), which would contain the code you want to execute when the animation finishes.
According to your comment,
I tried this, and it works - except in order for
it to advance to the next point you have
to click the next button at exactly the right frame.
I'd like to make it so you can click it at any point in the Movieclip, and it
will play the remainder of the Movieclip before proceeding
to the next frame. Here's my code for what I've added: function
nextBtnUp(e:MouseEvent):void {
nextBtn.gotoAndPlay(5); if (MovieClip(root).animationTest.currentFrame ==
MovieClip(root).animationTest.totalFrames) MovieClip(root).nextFrame();
}
Thank you for the help so far
You have it most of it figured out, so to finish can add stop() in the code at the last frame of your MovieClips, or you can do this (the more complex version):
yourClip.yourFirstClip.addEventListener(Event.ENTER_FRAME, stopAtLastFrame);
yourClip.yourSecondClip.addEventListener(Event.ENTER_FRAME, stopAtLastFrame);
function stopAtLastFrame(e:Event):void
{
if(e.currentTarget.currentFrame === e.currentTarget.totalFrames)
// ^Note that no conversion is being made, so you can have 3 equal signs
{
e.currentTarget.stop();
}
}

Can I specify a delay before the browser raises "rollover" event?

I am working on an ASP.NET web application that is required to bring up a popup on a roolover. I am using the "OnMouseOver" event and it works as expected. The problem is that the event is on a "hair trigger"; even a casual passage of the mouse over the control brings up the popup (which then must be manually dismissed). I want to add a delay so that a rapid pass over the control in question does not trigger the event. Is there a way to set such a delay or is there a different event that I could use to get the same "trigger event on a slow rollover"?
One solution that comes to mind, there may be better ways though:
Make the onmouseover call the function via a setTimeout delay
Inside the function, check the mouse is actually over that element.
You could also use an onmouseout to clear the setTimeout, but then you'd have to store a reference to the timer in a global variable to get at it again.
What I ended up doing is as follows (oRow is a table row but it could be any control):
function ItemMouseOver(oRow, "parameters for the popup")
{
oRow.showTimer = window.setTimeout(function()
{
alert('popup');
}, 1000);
}
function ItemMouseOut(oRow)
{
if (oRow.showTimer)
window.clearTimeout(oRow.showTimer);
In the ASP.NET grid view RowDataBound event: I added the following code:
protected void ReportGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && (
e.Row.RowState == DataControlRowState.Normal
|| e.Row.RowState == DataControlRowState.Alternate))
{
// get the input values for the popup for the row (stuff deleted)
e.Row.Attributes["onmouseover"] = "javascript:ItemMouseOver(this,
"parameters for the popup");";
e.Row.Attributes["onmouseout"] = "javascript:ItemMouseOut(this);";
}
}
It works just fine. Thanks.