Okay, so I came up with this brilliant idea earlier to practice ActionScript 3.0, I decided I would create a flappy bird clone. I have the basic bird movements down, so the bird can move up and down appropriately and he'll rotate and it looks really slick.
But I have a problem with a boolean variable and an if statement. It isn't really bothering the code being there but I would like to know why it is doing what it is doing. If you run this code below in flash with your own symbol called bird, it'll work fine. You can press any button and the bird will fly up and the fall back down.
var hasGameStarted:Boolean=false
//input stuff
stage.addEventListener(TouchEvent.TOUCH_TAP, tap1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, tap1);
function tap1(event):void{
if (!hasGameStarted){
startGame();
}
}
function startGame():void{
hasGameStarted=true;
var jumpLevel:int=10
var jumpLevelCap:int=36
removeEventListener(KeyboardEvent.KEY_DOWN, tap1);
removeEventListener(TouchEvent.TOUCH_TAP, tap1);
stage.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(TouchEvent.TOUCH_TAP, tap2);
stage.addEventListener(KeyboardEvent.KEY_DOWN, tap2);
function update(event):void{
bird.y+=jumpLevel;
//gravity
if (jumpLevel<jumpLevelCap){
jumpLevel+=6;
}
//rotation
if (bird.rotation<100 && bird.rotation>-90 && jumpLevel>0){
bird.rotation+=jumpLevel;
}
if (bird.rotation<100 && bird.rotation>-100 && jumpLevel<0){
bird.rotation+=jumpLevel*2;
}
if (bird.rotation>=100){
bird.rotation=99;
}
if (bird.rotation<-90){
bird.rotation=-89;
}
//out of bounds
if (bird.y<1){
killBird();
}
}
function tap2(event):void{
jumpLevel=-30;
}
function killBird():void{
}
}
However, if you run the exact same code with all things pertaining to the boolean variable hasGameStarted removed from the code or commented, the program will get ridiculously fast. There are no other variables depending on this boolean, and it is not needed at all.
//var hasGameStarted:Boolean=false
//input stuff
stage.addEventListener(TouchEvent.TOUCH_TAP, tap1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, tap1);
function tap1(event):void{
//if (!hasGameStarted){
startGame();
//}
}
function startGame():void{
//hasGameStarted=true;
...
If you need to know exactly what happens, it will pretty much function the same besides the bird will move ridiculously fast and will move faster every time you press a button.
Please help, I'm still learning, I started like a week ago.
You added the listener tap1 for TouchEvent.TOUCH_TAP to Stage, but in startGame you are removing it from the current object, not Stage (at lines 17 & 18). If you don't have that boolean, every tap call will call startGame.
The solution is to correctly remove the previous listeners:
stage.removeEventListener(KeyboardEvent.KEY_DOWN, tap1);
stage.removeEventListener(TouchEvent.TOUCH_TAP, tap1);
Related
I've run across a piece of code that I'm really struggling to get working... It's meant to wait three seconds before fading an object (swslogo), however when I test it, it doesn't seem to work.. anyone know why this might be
var GameMode:Number = 0;
swslogo.alpha = .0;
var IntroTimer = new Timer(4000,1); //add a 4 second timer
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
intro();
function intro(e:Event=null):void
{
IntroTimer.reset()
IntroTimer.start();
}
function swsfadein(e:Event=null):void
{
IntroTimer.stop();
swslogo.addEventListener(Event.ENTER_FRAME, fadein)
}
function fadein(e:Event=null){
if(swslogo.alpha <=0){
this.alpha +=0.1;
if(swslogo.alpha >=1){
this.removeEventListener(Event.ENTER_FRAME, fadein);
}
}
}
Edit: Removed accidental line that wasn't meant to be there
From your last few questions I can tell you totally need to learn what a program is, in general, unrelated to AS3 of any other given language, instead of struggling with random pieces of code while treating them like magic spells.
// This part is fine.
var IntroTimer = new Timer(4000,1); //add a 4 second timer
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
// A method is called. It resets and restarts timer.
intro();
// Another method is called. It stops the times and starts the fading thing.
swsfadein ();
So basically that code in a single go:
Creates timer.
Starts timer.
Stops timer.
Starts fading.
Which obviously suppresses the intended use of the timer.
I've inherited a large, legacy Flex project and the deeper I get into the code, the more concerned I am becoming. For example, I am looking at code for a "window" type image viewer within the app. Every time it is displayed, the eventListeners below are added and never removed.
Since these are strong references and never removed, that is one problem but this repeatedly adding eventListeners is giving me pause. The "window" can be displayed and hidden many times in the lifecycle of the app.
My question: does this mean that is has n = (4 * number of times displayed) eventListeners? (...shudder).
This is a huge project revision on a tight budget so I am trying to determine if I fix this sort of thing or just let it go.
addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
If they are different eventlisteners, they will be added multiple times. If they all refer to the same function, it will be overridden each time, calling the specific function only once.
try out the following short example to see what i mean:
var s:Sprite = new Sprite(); //some sort of displayobject with EventDispatcher capabilities
s.addEventListener(MouseEvent.CLICK, onClick);
s.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void{
trace("hey");
}
pressing on the Sprite will give you a console output of "hey", not two "hey"s.
Now consider the following:
var s:Sprite = new Sprite();
s.addEventListener(MouseEvent.CLICK, onClick);
s.addEventListener(MouseEvent.CLICK, onClick2);
function onClick(e:MouseEvent):void{
trace("hey");
}
function onClick2(e:MouseEvent):void{
trace("sup");
}
This will give you an output of "hey" and "sup" once you press on the Sprite.
If you are really concerned, you could just give the event listener a weak reference. I don't know how complex the project is you're working on, but implementing something to get rid of all eventlisteners at once (like, waiting for Event.REMOVED_FROM_STAGE and then manually removing the listeners) shouldn't be too time-intensive.
I have some code in a frame. It's basically
this.addEventListener(Event.ENTER_FRAME, handleUpdate);
function handleUpdate(e:Event):void
{...}
I want the code to be executed only when on that frame. But the handleUpdate function keeps getting called even when I'm out of that frame.
The timeline is stopped on this frame, and I want the handleUpdate to run continuously until the timeline moves off the frame.
If you're set on having the code for this on the frame in question, then you could do this:
var tmpCurFrame:int = currentFrame; //store the current frame
this.addEventListener(Event.ENTER_FRAME, handleUpdate)
function handleUpdate(e:Event):void {
if (tmpCurFrame != currentFrame) { //if the frame has changed, stop the frame handler
this.removeEventListener(Event.ENTER_FRAME, handleUpdate);
return;
}
//do your code
}
handleUpdate(null);
As an aside, it's much cleaner to have a document class and other class files that manage this sort of thing instead of using frame scripts. But if you all you're looking for is a quick and dirty tweak to your existing code, this should do the trick.
Haven't you heard about addFrameScript ?
It's perfect for your needs.
var desiredFrame = 25; // Timeline frame (starts from 1)
this.addFrameScript(desiredFrame-1, onFrame25); // 1st param is zero-based
function onFrame25():void
{
trace("I'm on frame", desiredFrame);
}
There's a few things you should consider with your approach:
Adding an ENTER_FRAME listener on the frame you care about happens after you enter that frame, so if the movieclip is playing you won't get an ENTER_FRAME event until the next frame (at which time it may have moved off that frame).
Be aware that code on a frame executes every time the playhead enters that frame, and you should be careful to remove listeners when appropriate for memory leak purposes.
So one approach would be to place this code on the frame in question - note that it also nicely removes its listener:
var thisFrame:int = currentFrame;
function handleUpdate(e:Event) {
if (currentFrame==thisFrame) {
// your code here...
} else {
// remove listener if we moved off the frame
removeEventListener(Event.ENTER_FRAME, handleUpdate);
}
}
// call it now because the listener won't fire until next frame
handleUpdate(null);
// add listener in prep for next ENTER_FRAME, though note that
// if we move off this frame, then the listener is removed above
addEventListener(Event.ENTER_FRAME, handleUpdate);
Another approach would be adding the following code on frame 1, so the listener always runs and is never cleaned up, and only performs the code when on frame 12:
addEventListener(Event.ENTER_FRAME, handleUpdate);
function handleUpdate(e:Event):void
{
if (currentFrame==12) {
// your code here...
}
}
I have a Movie Clip, and I need to stop the movie when it's reaches a certain frame. To do this, I need to add an event listener that will dispatch when that specific movie clip enters a new frame (as opposed to the whole animation ENTER_FRAME).
bar.addEventListener(Event.ENTER_FRAME, function(e:Event) {
var b:MovieClip = MovieClip(e.currentTarget);
if(b.currentFrame == mp.getPopularity())
b.stop();
});
Any ideas?
Thanks.
Try this;
A simple function:
function playUntil(target:MovieClip, frame:int):void
{
target.stopFrame = frame; // Works because MovieClip is dynamic.
target.addEventListener(Event.ENTER_FRAME, _checkFrame);
}
With the listening function:
function _checkFrame(e:Event):void
{
var m:MovieClip = e.target as MovieClip;
if(m.currentFrame == m.stopFrame)
{
m.stop();
m.removeEventListener(Event.ENTER_FRAME, _checkFrame);
}
}
Apply it to all your instances:
playUntil(instance1, 15);
playUntil(instance2, 27);
playUntil(instance3, 113);
You should remove the event listener when it's no longer needed, otherwise you risk memory leaks. An anonymous function can make this difficult, though you might be able to do it with arguments.callee.
bar.addEventListener(Event.ENTER_FRAME, function(e:Event) {
var b:MovieClip = MovieClip(e.currentTarget);
if(b.currentFrame == mp.getPopularity()){
b.stop();
b.removeEventListener(e.type, arguments.callee);
// ^^ this may work to remove your anonymous listener.
}
});
There's another way to go about this, though. Does mp.getPopularity() change frequently? If it does not change after bar is told to play() then you could use addFrameScript. Just remember that addFrameScript is 0-indexed, so adding a script to frame 1 means you have to pass 0... so if an action is to happen on mp.getPopularity() then you'll have to pass mp.getPopularity() - 1.
var framescript:Function = function():void{
bar.stop();
bar.addFrameScript(bar.currentFrame-1, null);
// ^^ nulling the framescript after
// ^^ it is no longer needed.
}
bar.stop(); // Generally a good idea to call stop before calling addFrameScript
bar.addFrameScript(mp.getPopularity() - 1, framescript);
bar.gotoAndPlay(1); // or wherever it needs to start from.
This is a more precise solution, but you do have to remember to clean up your framescript after you're done, if you plan to use this same bar instance later with a different mp.getPopularity() value.
Ok i have a mc called dracoplay and inside is another mc called Drakep and inside that there is another mc called wing and head as you may have guessed they make up a dragon. So dracoplay is the character that gets coded. Drakp is the body and the head and wing have animations. Now my problem is making it breath fire so that it will keep firing until i let go of the left click (I already found a way for it fire when i click but i have to click again for it to stop). I would also like for it to shoot towards the mouse.
from what i'm seeing, you should use two eventListeners, a mouseDown and mouseUp-event :) I'll write some basic code that should help you.
public function constructor():void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, toggleFire);
stage.addEventListener(MouseEvent.MOUSE_UP, toggleFire);
}
these two eventListeners should be added in the constructor of your code. Then, the toggleFire-method will look like this:
private function toggleFire(e:MouseEvent):void
{
if(e.type == MouseEvent.MOUSE_DOWN)
stage.addEventListener(Event.ENTER_FRAME, breatheFire);
else
stage.removeEventListener(Event.ENTER_FRAME, breatheFire);
}
the last function you need is the breatheFire-function.
private function breatheFire(e:Event):void
{
//Write your code for the fire breathing here
}
and this should do the trick.