Flash CS6 AS3: Else If statement - actionscript-3

I explained this earlier and got a good answer. For some reason it's not working out as I want it to. I have a counter counting down from 15 seconds. I want the users to input " 4 8 15 16 23 42" and then click a button or hit enter. This would then launch a different scene bumping the counter back up to 1:08:00..
But if they don't put the numbers in before the counter hit zero I want it to display a different scene of the computer blowing up or shaking violently. Here is the code I got earlier. But once I hit the button it just restarts at 15 seconds opposed to playing my new scene.
EDIT: Just found out if you type in any numbers, not even correct ones, it starts the counter back at 15.
import flash.events.MouseEvent;
submitbtn.addEventListener(MouseEvent.CLICK, testPassword);
function testPassword(e:MouseEvent):void { if (numbertext.text == "4 8 15 16 23 42") { gotoAndPlay("success");
} else { gotoAndPlay("shake");
}
}

First thing, is that this will work only if you click with the mouse, so you should add the listener if you want to add the possibility to press enter,
and gotoAndPlay in as3 is like gotoAndPlay(frameNumber, "sceneName");
so your function should be :
import flash.events.MouseEvent;
submitbtn.addEventListener(MouseEvent.CLICK, testPassword);
function testPassword(e:MouseEvent):void {
if (numbertext.text == "4 8 15 16 23 42") {
gotoAndPlay(1, "success");
} else {
gotoAndPlay(1, "shake");
}
}
And about the timer, be sure that when you instanciate it, you do it this way,
var timer:Timer = new Timer(15000,1); // 15sec and just 1 time
and the listener should be TimerEvent.TIMER_COMPLETE
But what you need to do, is to read the documentation, may be some tutorials too, to learn the basics ...

Related

Actionscript 3.0: How to create fast forward button without skipping any code written on particular frame?

I've made 3 layers.
1 layer as follows:
I've created fastForward & fastPrevious buttons using
**
> function whatever()
{
frame=currentFrame+90;
this.gotoAndPlay(frame);
}
**
I wrote down this code in the beginning of this layer 1.
Layer 2 as follows:
stop();
I wrote down this code somewhere in middle frame.
Layer 3 as follows:
Only butttons for layer 3.
This code makes animation jump/next frame by 90 frames, but it also skips the code written(in layer 2) in between these jumps/next. If I write some code on frame 120 or elsewhere then it skips that code and jump to next designated frame while clicking btnfastForward. Simply want to make some bug free fastForward & previous button.
Pretty simple. You just need to go through all the frames in between.
function fastForward(targetFrame:int, mustPlay:Boolean = true):void
{
// Go to designated frame without skipping any.
while (currenFrame < targetFrame)
{
nextFrame();
}
// The "nextFrame" method also performs "stop".
// Resume playback if needed.
if (mustPlay)
{
play();
}
else
{
stop();
}
}

as3 timer turn external device on and off

I am trying to program a Phidgets device to repeatedly turn an external light on for .5 sec and off for .5 sec.
(these times correspond to Frames 1 and 11 of a 20 frame movie clip.
I've tried to insert the on- and off- commands into the movie clip, but they don't transfer to the main stage, so now I'm just trying to program the light to go on and off from the main program
The code below turns the light on and off at the SAME TIME- which means NOTHING HAPPENS.
Does anyone know how to delay the timer so that the off command comes 500 msec AFTER the on command ?
THanks
phid.addEventListener(PhidgetEvent.ATTACH,onAttach);
//connects Phidget to software
function onAttach(evt:PhidgetEvent):void{
trace(evt);
}
var phidControl:Timer = new Timer (500);
phidControl.addEventListener(TimerEvent.TIMER, lightOn);
function lightOn (evt:TimerEvent):void {
phid.setOutputState(0, true);} //this turns the light on
enter code here
var phidOff:Timer = new Timer(500);
phidControl.addEventListener(TimerEvent.TIMER, lightOff);
function lightOff(evt:TimerEvent):void {
phid.setOutputState(0, false);//this turns light off
}
it looks like you're creating two Timers which both run at a 500ms interval simultaneously.
It might be easier to put one function on a 500ms timer, which will call either function intermittently, something like
var lightIsOn = false;
function lightToggle(evt:TimerEvent):void{
if(lightIsOn){
lightOff();
}else{
lightOn();
}
lightIsOn = !lightIsOn;
}
---- UPDATE ----
I'm not very familiar with Phidgets, however that shouldn't make a difference in this case. Try this:
val = true;
var phidControl:Timer = new Timer(500); //If you don't set the repeat count it goes on forever
phidControl.addEventListener(TimerEvent.TIMER, lightToggle);
function lightToggle(evt:TimerEvent):void {
val = !val;
phid.setOutputState(0,val);
}
phidControl.start(); //Make sure you start the timer.
If you only want to switch it once every .5 seconds then you don't have to worry about reading the state of the light, just tell it when to change. So from what I can tell, you don't need the inputChange function.

Actionscript 3.0 repeating movie three times then running close frames

I have an animation that I want to loop three times and then finish the final frames and then stop. I have tried this code to start with:
var stopNum = 0;
function looper(loopLimit) {
if (stopNum>=loopLimit) {
stop();
} else {
gotoAndPlay(2);
}
this.stopNum++;
}
And this code to stop:
if (!loopCount) {
var loopCount:Number = 0;
}
loopCount++;
if (loopCount >= 3) {
this.stop();
}
I have remaining frames to play from this point and then the entire animation stops. The problem is the frames loop three times but include all the frames including the closing frames.
Try using some events available from Flash 10 like so:
Event.FRAME_CONSTRUCTED
and
Event.EXIT_FRAME
Throwing some idea though, e.g. If your animation ends, then use Event.EXIT_FRAME and keep counter which will tell you that how many times animation is played.
Off the top of my head, I'd use an event listener to check for when the playhead advances for the moiveclip (as mentioned by Rajneesh). Within the event listener, I'd check for what frame it is at, and if it is at the end of my 'end loop frame' and then I'd check if I need to loop it. If so, then I increment a counter to keep track of how many times I've looped, and tell the movieclip to go to the start frame and play again.
Once it's looped enough times, I'd just let it run until the last frame then, on which I'd stop the animation.
I'll take a guess and assume your movieclip has 100 frames, and you only want frame 1 to 90 to loop 2 times, then have it play 1 more time, but from frame 1 to 100. Plays a total of three times from 1 to 90, then once 91 to 100, before stopping.
import flash.display.MovieClip;
import flash.events.Event;
var clip:MovieClip = this.aCircle;
var timesPlayed:int = 0;
var timesToLoop:int = 3;
var frameToStartLoop:int = 1;
var frameToStopLoop:int = 90;
function enterFrameListener(inputEvent:Event):void {
if(clip.currentFrame == frameToStopLoop){
timesPlayed++;
if(timesPlayed < timesToLoop){
clip.gotoAndPlay(frameToStartLoop);
}
// if the currentFrame made it past the above condition, then
// it means there is no more looping needed so just play till the end and stop.
} else if(clip.currentFrame == clip.totalFrames){
clip.stop();
// can remove this listener now, as it is no longer needed
clip.removeEventListener(Event.ENTER_FRAME, enterFrameListener, false);
}
}
// use an event listener to listen for the ENTER_FRAME event, which is triggered everytime the movieclip advances a frame
// (as a side note, you'll also find that this event is triggerd even if there is only 1 frame, or even if the playhead is not actually moving, check the doc for details)
clip.addEventListener(Event.ENTER_FRAME, enterFrameListener, false, 0, true);

Removing Children in AS3

My flash game exists of a timeline with multiple frames (I know I should avoid the timeline)
The point of the game is a point and click adventure. The object that you are able to pick up get spawned and destroyed accordingly as you enter and leave the room. now my problem is when entering frame 14 (accessibel from frame 12) it creates a piece of paper which you are able to pick up if you have another item. Now my problem is when you can't or don't pick up the paper and go back to frame 12 (only exit is to frame 12), you can't click on any other object and you are basicly stuck on frame 12. When leaving and entering other rooms it works properly but for some reason it doesn't for on the paper on frame 14.
My code to remove objects works as following
In my Main.as Documentclass I have a function that called as soon as the game starts which does the following
if (lastframe == 14)
{
trace (prop.numChildren);
while (prop.numChildren )
{
prop.removeChildAt(0);
}
}
The lastframe variable is established when moving from frames
this function is found on the frame itself (each exit function on it's own respective frame)
function exitKantine(event:MouseEvent):void
{
Main.lastframe = 14;
gotoAndStop(12);
}
The function to remove the prop actually removes it but then causes all other clickable objects to be unusable.
Thanks for looking at my question and thanks in advance for your suggestions
I would say instead of removing children, add it once in the beginning, add all the listeners in the beginning, and toggle the visibility instead of trying to addChild and removeChild every time you want to hide it. Use an array so you can have a few happening at the same time.
something like this:
private function init():void
{
assignVars();
addListeners();
stage.addChild // make sure this is in document class or you are passing stage to the class using it
}
for (var i = 0; i < _thingsAry.length; i++)
{
if (_thingsAry[i] == 14)
{
_thingsAry[i].visible = false;
trace("the visibility of _thingsAry[" + i + "] is " + _thingsAry[i].visible
}
}

Play MovieClip frame by frame in addition after same function is executed several times

I have a MovieClip named fails_mc, it consists of 3 soccer balls and in my game every time my player fails I need to add an X mark over 1 ball, I have a hitTestObject detecting when the ball hits the goal area, but when the ball goes outside I need to play 1 frame inside the fails_mc and add the X mark to a ball ( I have 3 different png files inside fails_mc, 1 on each frame), this is the code I’m using but I don’t know how to play frame by frame the fails_mc (keep in mind that the same function will be used every time, that’s why each frame must be added to the last played frame:
if (ball.hitTestObject(goalie))
{
goal_mc.play();
net_mc.play();
}
else
{
fails_mc.play(+=1); // This is not working
trace("It’s a fail");
}
After 3 fails I must trigger another function that will finish the game but I will figure out how to do that later on.
I think all your looking for is the nextFrame() function of a movieClip.
you could also use gotoAndStop("x1") - where "x1" is the frame label (or number if not in quotes) you want the movieClip to goto. You could use a variable to track the current state.
var misses:int = 0;
if (ball.hitTestObject(goalie))
{
goal_mc.play();
net_mc.play();
}
else
{
misses++;
trace("It’s a fail");
if(misses > 3){
//do your game over stuff
}else{
fails_mc.gotoAndStop(misses)
}
}