Animate CC advances to the next frame with gotoAndStop commented out? - actionscript-3

I'm writing this code that tests your reaction time and then advances to the next frame. It shows a box and then time the difference between when the box appeared and when the use presses [A]. Heer is my code
import flash.utils.Timer;
import flash.events.Event;
import flash.utils.getTimer;
stop();
var canPress = false;
var startClock:Timer = new Timer(4000+Math.random()*6000, 1);
grbox.y = -500;
startClock.start();
var startTime:int = 0;
function displayBox(evt:Event):void{
canPress = true;
grbox.y = 143;
var startTime:int = getTimer();
}
function Tpressed(e:KeyboardEvent):void
{
if(e.keyCode==Keyboard.A){
if(canPress==true){
var endTime:int = getTimer();
score1 = endTime-startTime;
if(score2<0){
//gotoAndStop(3);
}
else{
//gotoAndStop(4);
}
}
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, Tpressed);
startClock.addEventListener(TimerEvent.TIMER, displayBox);
For some reason if I just spam the [A] button it will advance to the next frame. Why is this happening?!?! My 'gotoAndStop(4);' command is commented out so it should do anything, yet it is.
EDIT: Here is my .fla file: https://drive.google.com/open?id=0BxtLreFIVnSWR2VPSGdSaHZGaVk
RAW CODE: https://docs.google.com/document/d/1GRZIaKAdRNu3z3aPjjXNcgqMl2BhR-ZBT6gU7OeSbWQ/edit?usp=sharing

On one of your frames you added an event listener for key presses to the stage. That's probably where your problem is at. So when you press any key, it calls the pressed function as well as the Tpressed function. And since the key that is being checked for in each function is "A", both functions execute their if blocks. And both if blocks call a gotoAndStop method.
Without knowing exactly what you are trying to accomplish in the big picture, this problem could be fixed by removing the event listener for the pressed function when you leave that frame.
Could look like:
function pressed(e:KeyboardEvent):void
{
if(e.keyCode==Keyboard.A){
gotoAndStop(Math.round(Math.random()+2));
// remove the event listener since we are leaving this frame and you apparently only want this function to work on this frame
stage.removeEventListener(KeyboardEvent.KEY_DOWN, pressed);
}
}

Related

Custom Mouse Cursor dropping duplicate symbols after its been removed

first of all, I'm a total noob to as3 and coding in general, I barely operate outside of code snippets.
I'm working on a project, and part of which is a scene where you get a custom mouse cursor upon entering the scene, and when you leave the scene, the custom mouse cursor is removed. The code I'm using to start the custom cursor is:
stage.addChild(crsTemple);
crsTemple.mouseEnabled = false;
crsTemple.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event)
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
}
Mouse.hide();
with crsTemple being the instance name for the custom cursor. Then, when a new scene is entered (via rolling over an object), i have the following code in the new scene:
stage.addChild(crsTemple);
crsTemple.mouseEnabled = false;
crsTemple.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_4);
function fl_CustomMouseCursor_4(event:Event)
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
}
Mouse.hide();
crsTemple.removeEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_4);
stage.removeChild(crsTemple);
Mouse.show();
Unfortunately, whenever I go into the second scene, I get the regular mouse again, but it drops the crsTemple wherever the mouse was when the scene change happened, and it stays there for the rest of the time the file is running.
Any help is greatly appreciated, much thanks in advance for helping a noob like me!
No need to write the same code in new Scene. You can actually use all declarations form the first Scene. In the following code snippet MOUSE_MOVE handler (fl_CustomMouseCursor) from scene 1 will be called in scene 2 either. Custom cursor will also be accessible by its name crsTemple.
import flash.display.MovieClip;
import flash.events.MouseEvent;
var crsTemple:Sprite = new CrsTemple();
crsTemple.mouseEnabled = false;
addChild(crsTemple);
// for smooth cursor movement MOUSE_MOVE instead of ENTER_FRAME
stage.addEventListener(MouseEvent.MOUSE_MOVE, fl_CustomMouseCursor);
stage.addEventListener(MouseEvent.CLICK, nextStage); // for test purpose, just to switch the stage
function fl_CustomMouseCursor(event:Event):void
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
trace(crsTemple.x);
}
function nextStage(e:Event):void {
gotoAndStop(1,"Scene 2");
}
Mouse.hide();
stop();
here is a link to fla sample

Adobe Flash/ActionScript 3.0 moving to next scene but nothing plays

I've searched endlessly for an answer but can't seem to find one.
I'm building a flash presentation for a client. It's got about 15 scenes, the .fla file is 200MB before publishing (26MB afterward). It's a pretty simple operation; I'have a pause/play & replay button, and once the scene is completed a next & replay button appears in the center of the stage.
My issue is when I get to about the 8th scene, halfway through. All my tweens and buttons stop working. Simple fade-in text doesn't come up and my pause/play & replay no longer function. I've tried shifting around scenes to see if it was anything in particular, but no matter what order and what scenes it always stops halfway through the 8th. I don't get any error notifications before, after or during the play. Tracing tells me that the button has been clicked and it's moved to the next scene but it simply will not play. Adding a play(); comment at the first frame of the next scene has not helped either.
Here are my functions that are on Scene 1 Frame 1
function pause_movie(event:MouseEvent):void {
stop();
playBtn.visible = true;
pauseBtn.visible = false;
}
function play_movie(event:MouseEvent):void {
play();
playBtn.visible = false;
pauseBtn.visible = true;
}
function replay_movie(event:MouseEvent):void {
gotoAndPlay(1);
}
function next_movie(event:MouseEvent):void {
this.nextScene();
trace("next_movie " + this.currentScene.name);
}
And then I just add event listeners when my buttons appear per scene
import flash.events.MouseEvent;
//Hide play button to start
playBtn.enabled = true;
pauseBtn.enabled = true;
playBtn.visible = false;
pauseBtn.addEventListener(MouseEvent.CLICK, pause_movie);
playBtn.addEventListener(MouseEvent.CLICK, play_movie);
replayBtn.addEventListener(MouseEvent.CLICK, replay_movie);
Any help is appreciated! Thank you!
I've created a new flash file with a blank canvas (just page number and next button). I'm loading the audio externally now as per #VC.One 's comment and the program stops working at the same scene regardless of which audio file I put in there.
Here is my updated code:
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
var channel:SoundChannel = new SoundChannel;
var s:Sound = new Sound();
function newSound() {
s.removeEventListener(Event.COMPLETE, onSoundLoaded);
s = new Sound();
s.addEventListener(Event.COMPLETE, onSoundLoaded);
}
function onSoundLoaded(e:Event):void
{
channel = s.play();
}
function next_movie(event:MouseEvent):void
{
channel.stop();
try {
s.close();
}
catch(e:Error) {
trace("File already loaded");
}
this.nextScene();
trace("next_movie " + this.currentScene.name);
}
and each scene starts with:
pageNum.text = this.currentScene.name;
skipBtn.addEventListener(MouseEvent.CLICK, next_movie);
newSound();
s.load(new URLRequest('audio/ISB page 17 tk 1.mp3'));
Finally solved this issue. The scenes added together were more than the 16k frames Flash is apparently limited to. I had to convert each scene into a movieclip and put them in each of their own frame all in one scene with a bit of coding to play each one after the previous one finished. Everything works fine now. Thanks guys!

Sound won't stop when I go back to a previous Stage with code

i have 4 scenes and i put my script for music in scene 2 and then i jump to scene 3 but when i go back to scene 2 and press pause or stop, my music wont do that, but when i play, the music play and i get 2 music start :(, can anybody help the script ?
regards im newbie,
stop();
import flash.events.MouseEvent;
import flash.media.SoundChannel;
//declaring all variables
var isPlaying:Boolean = false;// boolean type of variables can be true or false only
var myMusic = new soothing();// saving music in a varaible
var myChannel:SoundChannel = new SoundChannel();// sound channel Class to stop
var lastPosition:Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
stop_btn.addEventListener(MouseEvent.CLICK, onStopClick);
function onPlayClick(event:MouseEvent):void
{
if (isPlaying == false)
{
isPlaying = true;
myChannel = myMusic.play(lastPosition);
}
myChannel.addEventListener(Event.SOUND_COMPLETE, completeHANDLER);
function completeHANDLER(event:Event):void
{
lastPosition = 0;
isPlaying = false;
}
}
function onPauseClick(event:MouseEvent):void
{
isPlaying = false;
lastPosition = myChannel.position;
myChannel.stop();
}
function onStopClick(event:MouseEvent):void
{
if (isPlaying == true)
{
isPlaying = false;
lastPosition = 0;
myChannel.stop();
}
}
homebtn.addEventListener(MouseEvent.CLICK, qnextScene);
function qnextScene(event:MouseEvent):void
{
gotoAndStop(1, "Scene 2");
}
gallerybtn.addEventListener(MouseEvent.CLICK, nxtScene);
function nxtScene(event:MouseEvent):void
{
gotoAndStop(1, "Scene 3");
}
mebtn.addEventListener(MouseEvent.CLICK, nextsScene);
function nextsScene(event:MouseEvent):void
{
gotoAndStop(1, "Scene 4");
}
Ahhh, yes, the infamous stage sound bug I spent four months chasing down.
One fix: DON'T GO BACKWARDS ON STAGES. They're quite buggy.
Instead, what you put on a given stage, instead put inside a MovieClip. Create two functions instead each of those major MovieClips, one to start all the sounds, animations, etc that need to happen when it appears. The other to STOP sounds, animations, etc.
Then, set up a SINGLE stage with code to swap between these.
One other issue you're going to run into when you start in on this approach is that variables will seemingly clear themselves spontaneously. This is due to the nature of the timeline.These master movieclips should have ONE FRAME ONLY, and stop(); must be the first line of code after the import. If you have multiple frames on these master movieclips, or if you neglect stop, things will keep re-initializing, which is a pain in the butt.
(Heading this off at the pass: yes, code on the timeline is
acceptable, as long as all the code relates directly to the object it
is on the timeline OF. Pure code should be parked in the document
class and linked classes. The idea that you should never put code on
the timeline is a relic practice from AS2.)

replay button in AS3

I am making an e-learning module with multiple frames.
I want to add a refreshbutton, so that a user can reload a frame (with a movieclip), so that he or she can watch it again. I use one layer where I place all my actions.
I tried, the following, but that doesn't work
refresh_btn.addEventListener(MouseEvent.MOUSE_DOWN, goToCurrentPageHandler) ;
function goToCurrentPageHandler (event:MouseEvent) : void
{
SoundMixer.stopAll();
gotoAndPlay();
I also tried:
/*refresh_button*/
refresh_btn.addEventListener(MouseEvent.MOUSE_DOWN, goToCurrentPageHandler) ;
function goToCurrentPageHandler (event:MouseEvent) : void
{
SoundMixer.stopAll();
gotoAndPlay(currentFrame);
But when I press the refresh button it starts playing the next frame.
Can someone please help me.
Thanks!
without refreshing you can try simply insert stop button function inside play buttons function then no need to refresh you should first stop sound chaneel sc inorder to close sound s . then disable and enable play and stop buttons by //object..mouseEnabled = false; , //object..mouseEnabled = true;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
import flash.events.Event;
btn_play.addEventListener(MouseEvent.CLICK, PlayStream);
var sc: SoundChannel = new SoundChannel();
var s: Sound = new Sound(new URLRequest("folder/song .mp3"));
function PlayStream(event: MouseEvent): void {
sc = s.play();
btn_play.mouseEnabled = false;
btn_stop.mouseEnabled = true;
btn_stop.addEventListener(MouseEvent.CLICK, StopStream);
function StopStream(event: MouseEvent): void {
SoundMixer.stopAll();
sc.stop();
s.close();
btn_play.mouseEnabled = true;
btn_stop.mouseEnabled = false;
}
}
stop();
gotoAndPlay(currentFrame);
actually plays the next one because you are saying "get the current position and start playing from there". Your sound is in a movie clip called *frame_1*. So you should use:
frame_1.gotoAndPlay(1);
I.e. your code should look like that:
/*refresh_button*/
refresh_btn.addEventListener(MouseEvent.MOUSE_DOWN, goToCurrentPageHandler) ;
function goToCurrentPageHandler (event:MouseEvent) : void
{
SoundMixer.stopAll();
frame_1.gotoAndPlay(1);
}

AS3 button firing off multiple times if clicked fast only

ok so, i wrote this code:
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.display.Stage;
stop();
var loader:Loader = new Loader();
var defUrlReq = new URLRequest("indexCoontentLoad.swf");
var urlRequest:URLRequest = new URLRequest();
var myLoadedSwf:MovieClip = null;
var swfStage:Stage = this.stage;
/////////////// INITIATE LOADERS ///////////////
loader.load(defUrlReq);
/////////////// START MAIN HANDLER FUNCTION ///////////////
/////IMPORT DEFAULT SWF /////
loader.contentLoaderInfo.addEventListener(Event.INIT, loadedHandler);
function loadedHandler(event:Event){
myLoadedSwf = event.target.content;
addChild(myLoadedSwf);
trace(myLoadedSwf);
myLoadedSwf.gotoAndPlay("intro");
trace("STEP 1 -- ext def swf loaded");
}
///////END IMPORT. ///////////////
///// START LISTENERS AND THEIR FUNCTIONS /////
load1.addEventListener(MouseEvent.CLICK,btn4Clicked);
load2.addEventListener(MouseEvent.CLICK,btn4Clicked);
load3.addEventListener(MouseEvent.CLICK,btn4Clicked);
///// END LISTENERS /////
///// START FUNCTIONS /////
function btn4Clicked(e:MouseEvent):void { //-- START btn4Loaded
if (e.target == load1 || e.target == load2 || e.target == load3) {
myLoadedSwf.gotoAndPlay("outro");
removeChild(myLoadedSwf);
urlRequest = new URLRequest(e.target.name+".swf");
loader.load(urlRequest);
addChild(myLoadedSwf);
}
}
and it works, once clicked, it does what it has to do. Ofcourse, me trying to break it, i found that if i click the buttons fast, it will re-import the external swfs causing me to have multiple instances of the external swf.
so in short, if i click like normal(slow ) ie like a person that clicked to view a section etc, then its fine, if i click fast or repeated clicking ie like a person that double clicks etc, then the problem occurs.
any ideas how to fix this?
thanks in advance.
edit*** heres a link to test file to show what i mean
http://www.somdowprod.net/4testing/flash/tst
When you set doubleClick to enabled on your movieclip, this will work. The Flash runtime will thencheck for you if it is a double click and only trigger your method once. If you want to listen for the double clicks, you can by changing the event handler.
mySprite.doubleClickEnabled = true;
mySprite.addEventHandler(MouseEvent.CLICK, onClick);
Good luck.
You could try adding a boolean variable that is set to false. Once the .swf is loaded then change that variable to equal true. Then don't let the swf be loaded unless it is set to false. That way it'll only be allowed to be loaded once.
var isLoaded:Boolean = false;
function btn4Loaded(e:Event):void
{ //-- START btn4Loaded
if(!isLoaded)
{
if (e.target == load1 || e.target == load2) {
myLoadedSwf.gotoAndPlay("outro");
removeChild(myLoadedSwf);
urlRequest = new URLRequest(e.target.name+".swf");
loader.load(urlRequest);
addChild(myLoadedSwf);
isLoaded = false;
}
}
} // end btn4Loaded.