I am working on a Flash project with multiple scenes. I use the function:
function gameloop(e:Event) {
to add my functions in to one scene, but when switching the scene I am rained on with errors because the objects used in the other scene are no longer on screen. Is there an opposite to the code I pasted here to take functions out of the game loop for my other scenes?
function gameloop(e:Event) {
if(currentFrame != 2){
return;
}
}
Check currentFrame in the loop.
I'm assuming the way you're implementing gameLoop is via an Event.ENTER_FRAME listener.
If this is the case, then you simply need to remove the event listener:
stage.removeEventListener(Event.ENTER_FRAME, gameloop);
I'm using stage as an example, you'll want to change that to whatever object on which you originally added the listener.
Related
I would like to know how to create in Adobe Flash CS3 a button that will execute a function (like gotoAndPlay(51) but ONLY when playhead hit a defined keyframe from timeline.
Here is a drawing that does explain better what I want to do
So after I click the button, it will wait until the playhead hit the closest defined keyframe, only then it will execute my desired function.
Any help is appreciated.
There are a few ways to accomplish this. The most succinct way is to use an undocumented* feature called addFrameScript
What you can do with that method, is place code on a specific frame at runtime.
So, you could do something like:
//listen for the click on your button
myButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
//which frame should the gotoAndPlay run on?
var targetFrame:int = 19; //this is actually frame 20, since frames are 0 based in script
//this function will run on the above frame
function goto51(){
//goto frame 51
gotoAndPlay(51);
//remove the frame script
addFrameScript(targetFrame, null);
}
function btnClickHandler(e:Event) {
//use addFrameSCript to run the function goto51 on the target frame
addFrameScript(targetFrame, goto51);
}
*-one should point out that using undocumented features of a language comes with risk of the feature being taken out on a future version, however given the current life cycle stage of AS3, this is very unlikely
I have used some stage.addEventListener in my project in as3 but I want those stage events only to work on a specific frame. can I put the stage on false or something? how can I avoid that when I click a button on the stage other things/objects doesn't start doing things because they are related to the stage.addEventListener, can someone help me?
If you want to do something on a particular frame, Use following code.
stage.addEventListener(Event.ENTER_FRAME, chkit);
function chkit(event:Event):void{
if (currentFrame == 90){
trace("Reached"); // whatever you want to be done on reaching particular frame
}
else{
//things that should be done when particular frame is not reached
}
}
In your function, you can check the frame, like:
if(currentFrame == 'SpecificFrame')
{
//codeHere
}
A better way would to be to remove the event with removeEventListener when changing frames.
However, all children are part of the stage, so you you cannot prevent Objects related to the stage not to be effected by the Event. You can, however, make an Object the same size as the stage below all other Objects (through code, swapChildren or setChildIndex).
In my Starling game, the player sprite is controlled by clicking the screen where you want to go. Since (AFAIK) you can't have touch events that happen outside a sprite activate handlers in the sprite, I put the handlers in the game object. Here's a basic idea of how it goes:
To create my listener, I do this within my game object:
this.addEventListener(TouchEvent.TOUCH, onTouch);
But onTouch is never called (I even tried putting a trace() in there).
Am I missing something here?
Well, you can make events that happen outside of a sprite to trigger the sprite's handlers, for this, you attach listeners not to the sprite, but to some other object, usually stage. Check if the stage is available, though. The best way to handle stage present is a listener to ADDED_TO_STAGE event. You put that into your handler object's constructor, and put all stage-aware code into that listener.
public function Handler() {
if (stage) init(null);
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point. This is where should stage-aware code start
stage.addEventListener(TouchEvent.TOUCH, onTouch);
// at this point stage is populated and valid, thus we can use stage reference
// without fear of null pointer.
}
I have number of movieClips on stage, with each having it's own Event Listener. Once Click/Touch the event is called each movie clip does something. For example one movieClip makes about 6 other movie clips visible.
What I want to do is, when the user Touch/Click somewhere else on stage, where there is no movieClip I want to know, so I can perform some actions such as make some movieClips invisible.
P.S the reason why I say Touch/Click is I'm developing this app for Android, however to make testing easier I'm currently testing everything in PC with MouseEvent, rather than TouchEvent. Once I get all the features working, I will switch over to TouchEvent and test it in mobile.
Many Thanks,
Mike
Add event listener to the stage. And in the event handlers of your inner movieclips use event.stopPropagation function to prevent bubble event to the container.
I would just do a check, it's simple and quick and doesn't require adding code to alter propagation.
import flash.events.MouseEvent;
stage.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
function onClick(e:MouseEvent):void
{
if(e.target == stage)
{
trace("click click");
}
}
I want my movieclip to play once and stop on the last frame. I use the following code in the loop of my movieclip class. (this is as3)
if(currentFrame == 120)
stop();
120 is the last frame. It plays once. but the problem is it goes back to frame 1 again. is there a better method of stopping a movieclip on a particular frame.
If you want to do it with actionscript and not add a stop() to the last frame on the timeline manually, then you can use the undocumented addFrameScript() method.
mc.addFrameScript(mc.totalFrames - 1, function():void
{
mc.stop();
});
Can't remember the scope of the function, but you can either use mc.stop(), or just stop().
*EDIT - Added -1 to the first parameter of addFrameScript, because it is zero based (so to put a frameScript on frame 10 you would put 9).
On the timeline, you can just put a keyframe at the end, and add the stop(); method there. When it reaches that frame, the stop(); method will execute, and the clip will stop.
Assuming you are using the timeline that is, but sounds like you are.
Easiest way:
Just select the last Frame in the timeline. Open the Actions pane(F8). Ensure the frame u selected is still selected, if not then select again while the Actions pane is open.
After selecting, just add the simple stop() function in the the 'Action-Frame' pane.
//add the following line to the actions pane of the last frame.
stop();
You're done. You can optionally add a replay button if needed.
This seems more along the lines of what you're looking for:
addEventListener(Event.ENTER_FRAME, function(e:Event){
if(currentFrame == totalFrames){
stop();
removeEventListener(event.type, arguments.callee);
}
});
I added an event listener (for Actionscript 3) so on every frame this function will fire and check what frame it is on. If it's the last frame it will stop. Based on your example I'm presuming you have a class that's extending MovieClip.