AS3 Game loop and draw frame - actionscript-3

I am building a game for school in AS3. I made a game loop with a timer so this game can run faster.
private var updateTimer:Timer;
updateTimer = new Timer(0);
updateTimer.addEventListener(TimerEvent.TIMER, update);
updateTimer.start();
private function update(e:TimerEvent):void { }
This is working, but my teacher did show me a function/command how I can draw/render a frame.
Because of that the frame rate will go up. Unfortunately, I lost that code and I ca'nt find that function anywhere. Does anyone know that function? Yes, I know this way is very unstable.
Thanks a lot.

Are you thinking of TimerEvent.updateAfterEvent()? This causes Flash to render a new frame immediately independent of the movie frame rate in response to a timer or mouse event.
e.updateAfterEvent();

If you want to just change the frame rate of a game without messing with timers, you can always use stage.frameRate and go from there. So if you want to increase the frame rate, just use stage.frameRate++;.

Related

Actionscript-3 looping issue--Adobe Animate CC

My employer decided they wanted me to start doing animation with Adobe's new "Animate CC" application. My issue is that I don't know how to loop my animation outside of the Adobe Animate environment. I am new to Adobe Animate CC and ActionScript, unfortunately, so I will probably need a relatively basic answer to understand why my solution isn't working. From what I can tell, my ActionScript code is being ignored by the IDE completely.
In the IDE and in the browser test command, the animation plays beyond frame 100, to the end, and then flashes a frame of white before repeating. I need it to loop without this white frame interrupting the screen, whether that be through a loop or some other means that I'm just not aware of.
For context: my project has about 100 layers of content and I'm unfamiliar with how this program works. I've thoroughly searched the web for tutorials on how to do what I need to do, but I've come up empty handed.
I have an actions layer among my motion tweens and other layers
https://gyazo.com/6e0b8502d98b6c9903bb96ac3a939bae
I've been trying to use gotoAndPlay(0) at frame 100 to start the animation over from the beginning.
https://gyazo.com/704ee7158bae6dfd149b6283cfa33451
Basically, how do I use Action-Script in Adobe Animate CC in order to infinitely loop my animation until closed?
Thanks everyone.
Your flicker may be a result of having an extra blank keyframe on one of your layers.
Assuming that you don't have any additional scripts to stop your animation (e.g. stop()), the Timeline should loop automatically whether your animation is inside a MovieClip or on the main Timeline. You shouldn't have to put any script on your timeline or in a separate AS file to make an animation loop. I would suggest this method.
Additionally, although you have the code specifying that you want it to go the first frame, it will ignore your call because the timeline is still playing and therefore the priority. One way you can combat this is by adding a stop(); function and a delay timer that contains your gotoAndPlay(0) function. This will take focus away from playing the Timeline and will allow you to execute your script. I wouldn't suggest this method because it seems a bit redundant.
However, if you're curious one way that you could approach this is shown below, simply add this script to the frame you want the animation to restart at.
//Stop the Timeline
stop();
//Create a delay timer for 5 miliseconds that is executed once
var timer:Timer = new Timer(5,1);
//Add an event listener that calls once the timer is complete
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerHandler);
//Start the timer
timer.start();
//Timer handler that is called once the delay timer is complete
function timerHandler(event:TimerEvent){
//Go to and play the first frame
gotoAndPlay(0);
}

Flash AS3 | Looping sound for each animation set

with University done I've finally had time to go back to practicing my Flash work, and I need some help with getting sound to loop and change depending on what part of the animation is playing.
This is also partly a follow up question to an older one I asked, so look here to get more detail as to where I'm coming from if you don't understand! (I've solved that issue, the current one relates to following up on it!)
Flash AS3 | Finishing current animation set before code is executed
The situation is basic: I have two different scenes, one of a man walking and one of a man running. It loops seamlessly of the man walking until you hit a button, where it finishes the animation then starts looping the run animation. Likewise, hitting the button again, finishes the loop before going back to the looping walk animation. The code for the button is below.
import flash.events.MouseEvent;
Next2.addEventListener(MouseEvent.CLICK, Change_2);
function Change_2(event: MouseEvent): void
{
addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event):void {
if (currentFrame == 30) {
gotoAndPlay(31);
removeEventListener(Event.ENTER_FRAME, enterFrame);
}
}
}
My issue is trying to get a footstep sound to match up with these animations. So while the man is walking, the 'walking footsteps' sound clip will play and loop alongside the animation, and when the button is pushed and the animation moves into the running animation, the 'running footsteps'' sound will play.
Any help on this would be much appreciated. I'm terrible when it comes to anything audio/code based.
Well u can check about soundChannel. soundChannel can stop() and play() ur audio.
So import ur external sound or use internal one. ur code probably seems like this.
var mySound:Sound = new Sound(new URLRequest("YourSoundPath"));
var sc:SoundChannel = new SoundChannel();
//when u wanna play
sc = mySound.play();
//when u wanna stop
sc = mySound.stop();
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html
also look this link for more info
If you want to really MATCH the footsteps to the animation (for example, the step sound should play when the player have put his feet down) the only sure way to do that is to place the sound on a different layer where your animation is and set the sound to play as "stream" (if i remember correctly).
Otherwise the animation might be out of sync because of different computer capabilities, current computer or graphics card load etc.
When the sound is set to play as "stream" the flash player is orienting on the sound and drop frames if the graphics are slow to keep the sound and animation in sync

Overlapping sound in as3

Okay, so I am making a game, and I can't figure out how to keep the sound track from overlapping. When the game comes to a lose or win screen, I will click "Play again" and the sound will overlap. I have the sound on it's own layer. Here's what I have so far.
var snd_SolidState = new SolidState();
snd_SolidState.play(0,1);
And on Layer 2 Frame 2 and 3, I have:
snd_SolidState.stop();
snd_SolidState = null;
It says "stop" is not a function. So how would I get the song to stop on the lose/win screens? SolidState is the song.
If the code is related to the click of the "Play again" Button, here is the code for those.
mc_again.addEventListener(MouseEvent.MOUSE_UP,upAgain);
function upAgain(e:MouseEvent){
gotoAndStop(1);
}
Can anyone help?
You need to use a SoundChannel to stop your music.
The variable snd_SolidState is a Sound object, Sound doesn't have a stop method implemented; The Sound.play() however, returns a SoundChannel object which does...
So you can store that (the SoundChannel object returned by snd_SolidState.play()), which you can later use to stop the song.
PS: I recommend you start typing your variables (ie: var snd_SolidState:Sound;), that way you force yourself to know which object type you're dealing with and can find reference for it; in this case Sound.play().

How to code a play and pause button in flash AS3

I have made a simple game in flash. It includes a timer, falling objects and an object controlled by using swipe. So is there a way to stop every activity with the pause button and start it back again with the play button. Basically I want the whole stage to pause and play using buttons.
There is no way to do it automaticaly. You could base your entire game on a timer (even for animations and physics) and pause this timer. But this is a feature you have to think about during the very design of your game. Now I guess you just have to pause every movieclip, cancel your listeners to ENTER_FRAME and pause your timer.
Many game engines such as Flixel or FlashPunk include this feature
The way I do this in as3 is to run two timers, one that always runs at about 500ms and another that runs at framerate that varies with the speed of the game. Something like
public var speedControl:int = 15;
public var tick:Timer;
public function timers():void{
tick = new Timer(1000/speedControl);
tick.addEventListener(TimerEvent:TIMER,tickd);
tick.start();
}
public function tickd(t:TimerEvent):void{
// the timer has ticked
}
public function pause():void{
tick.stop();
}
public function play():void{
tick.start();
}
This is a basic example of course created on the fly, but you can call the pause or play button with a standard event listener. If your whole stage runs off of the timer, it will all stop. I use a second timer however that doesnt get stopped and started with the functions also. You really do want some things to run all the time.
If you wouldn't mind using the FP 11.8 Beta, it has that feature nativly to stop all movieclips recursively. Check it out here:
http://labs.adobe.com/technologies/flashruntimes/flashplayer/

stop object timeline from looping

I have a movie with several movieClips. One of the movieClips has a timeline with a motion tween. I created an instance of this movieClip:
var board:Board = new Board();
and used board.gotoAndStop(1); to keep the movieClip from playing at the beginning of the movie.
An eventListener checks to see when board collides with something; when it does, a function is invoked that uses board.gotoAndPlay(2) to get the board's timeline going.
I want the playhead on that timeline to stop at the end of the action, rather than looping. I tried to put a stop() on the last frame of that timeline, but Flash tells me I can't put an action on an object.
Can you help me solve this?
Thank you very, very much!
If memory serves, you need to make sure you have clicked in the frame in the timeline, so the frame is selected, and not an object on the stage. Then enter your actionscript.
Make sure you select a single frame where you would like to add actions then add your code
stop();
To make the timeline stop looping