Timer Not Working AS3 - actionscript-3

I am using a countdown timer.
what the game should do:
my first scene is the countdown and once the countdown reaches 0, it goes to the second scene
the problem is that the countdown will not show up but it will go to the first scene after 15 seconds which is what the countdown is counting down to. However when i renswe only the first scene using ctrl+alt+enter, the countdown shows up. I think the problem is some other code which is interfering.
There is a lot of coding and possible sources of error thereofore I have uploaded my game to the internet. Please do not use! This is the work of Pranavan Kantharajah for a school assignment!
Download to my flash game (AS3):
https://www.dropbox.com/s/039lkek494k65m8/EndOfPart1%20copy.fla

just add and complete event to your timer and then try to reset your timer there just like this:
var TM:Timer = new Timer(1000,15);
TM.addEventListener(TimerEvent.TIMER, do_my_job);
function do_my_job(e:TimerEvent):void
{
//
}
TM.addEventListener(TimerEvent.TIMER_COMPLETE, do_this_at_the_end);
function do_this_at_the_end(e:TimerEvent):void
{
TM.reset();
}
TM.start();

Related

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

AS3 timer not triggered correctly second time

I am trying to create a simple gallery. I have created a gallery movieclip that contains 4 frames with pictures. The above code works fine for one trigger. The gallery mc moves to frame 2 and the second image is shown. The next trigger though (after 2 more seconds) doesn't work properly. It gets stuck (eg other buttons of the stage aren't clickable any more). If I trace the NextPhoto function, it outputs two times and nothing more. What am I missing and the second trigger is not working?
import flash.utils.Timer;
stop();
gallery1.stop();
var myTimer:Timer = new Timer(2000);
myTimer.addEventListener(TimerEvent.TIMER, NextPhoto);
myTimer.start();
function NextPhoto(TimerEvent):void
{
if (gallery1.currentFrame < gallery1.totalFrames){
gallery1.gotoAndStop(gallery1.currentFrame+1);
}
else if (gallery1.currentFrame == gallery1.totalFrames){
gallery1.gotoAndStop(1);
}
}
I am not quite able to get your code as I'm a Flex kind of guy. But what I am able to guess from your code is that your Timer is not being restarted after it has executed for the first time. You should restart the timer for every frame, until the last frame to have the NextPhoto(e:TimerEvent) executed.
Thanks for the answes guys, eventually it was simply a memory issue.
When I Alt+Entered the project it didn't work. When I published at Projector, it worked.
Seems CS6 lately consumes too much memory, or I should upgrade my tower...

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/

AS3 Game loop and draw frame

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++;.

Flash CS6 AS3, Resetting Sound Channel

I've had a problem for a while trying to reset an audio loop to the beginning when loaded into Actionscript with this code i got from the official help forum
var alreadyExecuted:Boolean;
if(!alreadyExecuted){
alreadyExecuted=true;
var s:Sound=new SkaianSpirit();
var sc:SoundChannel=s.play(0,1);
}
What i want it to do, is loop indefinitely while the animation is playing regardless of positioning and whether or not frames have been skipped with buttons etc, but when the end of the animation comes, the track needs to end. At which point, if a button is clicked to send it back to the beginning of the animation, i want it to start the same loop as before.
Currently, it will do the first part, but will not replay.
A couple caveats:
I am not loading it through a url.
I am not using buttons to control audio volume etc. It is to play automatically when the animation starts.
I am a complete and utter novice with Actionscript. I will not know what you're talking about if you lambast me with jargon i won't understand.
I'd highly appreciate the help!
I'm always use addFrameScript function for synchronizing external sounds with animations:
UPD: mc is the name of your animation. Place this code in the place (the first frame of the parent Sprite that holds the animation for instance) where your animation is created.
var s:Sound=new SkaianSpirit();
var sc:SoundChannel;
mc.addFrameScript(1, startSound);
mc.addFrameScript(mc.totalFrames - 1, stopSound);
function startSound():void
{
sc = s.play();
}
function stopSound():void
{
sc.stop();
}