why is this script in as3 not working?? - actionscript-3

Hi I made this code and I use flash cs5.5
var cijfer_txt:int = parseInt(textarea_text.text);
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
submit.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler_2);
function fl_TapHandler_2(event:TouchEvent):void
{
switch (cijfer_txt){
case 1:
gotoAndStop(12);
break;
case 2:
gotoAndStop(23);
break; }
};
but I don't get it why it isn't working, the animation has to go to frame 12 when I fill in "1" and stop and has to got to frame 23 if I fill in "2" and stop but he doesn't do it and I get sick of it!!

Try to set cijfer_txt in fl_TapHandler_2
function fl_TapHandler_2(event:TouchEvent):void
{
cijfer_txt = parseInt(textarea_text.text);
switch (cijfer_txt){
}
}

I think you have problem with debugging so I'll help you:
First: change your code as follows -
var cijfer_txt:int;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
submit.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler_2);
function fl_TapHandler_2(event:TouchEvent):void
{
cijfer_txt = parseInt(textarea_text.text);
trace("in the function, cijfer_txt = "+cijfer_txt);
switch (cijfer_txt) {
case 1:
trace("in case 1");
gotoAndStop(12);
break;
case 2:
trace("in case 2");
gotoAndStop(23);
break;
default:
trace("in defaukt");
break;
}
}
NOW RUN IT AND WATCH THE CONSOLE\OUTPUT FOR TRACE OUTPUTS,
according to the output you can see what is hapening!
trace(); is a very common method
Good Luck
(Don't forget to mark as accepted if it helped you!)

Related

Event Listeners in If statements in AS3

I have a (maybe strange) Question for you experienced ActionScript gurus out there:
Logically maybe I am missing something but the situation is as follows: I have buttons nested on different frames of a MovieClip and on the attached class of the MovieClip I have an if statement that adds an Event Listener for the button nested on the appropriate frame; However when I run the code it seems that flash is executing the code in order regardless if the condition for running said code is true or not. I was just curious if there's a particular way to handle this sort of condition in a better way?
public function Die1() {
// constructor code
trace("Roll 1 D1 is:", MainGame.valueRoll1);
if (MainGame.valueRoll1 == 1){
gotoAndStop(1);
die1Sd1_btn.addEventListener(MouseEvent.CLICK, Side1process);
}
if (MainGame.valueRoll1 == 2){
gotoAndStop(2);
die1Sd2_btn.addEventListener(MouseEvent.CLICK, Side2process);
}
if (MainGame.valueRoll1 == 3){
gotoAndStop(3);
die1Sd3_btn.addEventListener(MouseEvent.CLICK, Side3process);
}
if (MainGame.valueRoll1 == 4){
gotoAndStop(4);
die1Sd4_btn.addEventListener(MouseEvent.CLICK, Side4process);
}
if (MainGame.valueRoll1 == 5){
gotoAndStop(5);
die1Sd5_btn.addEventListener(MouseEvent.CLICK, Side5process);
}
if (MainGame.valueRoll1 == 6){
gotoAndStop(6);
die1Sd6_btn.addEventListener(MouseEvent.CLICK, Side6process);
}
}
Thanks Ahead of time for anyone who replies to this post, I suppose all the syntax knowledge in the world cannot help with program logic...
You'd better use a single event listener on the entire die instead of using a lot of listeners that depend on the die's current frame. Like this:
function Die() {
this.addEventListener(MouseEvent.CLICK,processDie);
}
// instead of that entire 'if'!
// Also, this approach will allow you making more dies without spawning classes
function processDie(e:MouseEvent):void {
switch (currentFrame) {
case 1: side1Process(); break;
case 2: side2Process(); break;
case 3: side3Process(); break;
case 4: side4Process(); break;
case 5: side5Process(); break;
case 6: side6Process(); break;
}
}
And in your MainGame you do like this:
var dice:Array=[];
public function MainGame {
for (var i:int=0;i<maxDice;i++) {
var die:Die=new Die();
// make a new die. It'll automatically have an event listener attached
die.x=i*50;
die.y=20; // place somewhere
addChild(die);
dice.push(die);
}
// rest of code follows
}
Then, when you have to roll your dice, you do like this:
for (var i:int=0;i<dice.length;i++)
if (mayIRollTheDie(i)) // provide a correct condition here
dice[i].gotoAndStop(1+Math.floor(Math.random()*6));
Then your dice will change frames according to the roll of 1-6. Their currentFrame properties will reflect the roll, so you could easily gather them. And you will no longer need listening to Event.FRAME_CONSTRUCTED, because everything is now detached from current frame's objects.

as3 navigation - gotoAndPlay script not working consistently

I think I've made an error in logic somewhere but can't find it, I have a movie clip which contains 4 further animated movieclips, each with stop() actions at specific frames, and 2 buttons, 1 to move left and another to move right, the animations work as expected if the user clicks say right, right, right, right, left, left, left, left for the first time, but then things stop evaluating properly.
Am using switch case statements on a variable to pick out the mc to animate, would I be better of using an If Else statement? Heres my code, as may be obvious actionscript is not my forte!
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;
import fl.text.TLFTextField;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
// Global variables
var mcWidth:int = 768;
var mcPosInc:int = 0;
var boxAnimPlay:int = 1;
var mcMoving:Boolean = false;
var buttonClicked:String = "right";
var mcLeft;
var mcRight;
addEventListener(Event.ENTER_FRAME, frameHandler);
// frame Handler
function frameHandler(event:Event):void
{
// Initialize button click events
controlGrp.bttn_left.addEventListener(MouseEvent.CLICK, goLeft);
controlGrp.bttn_right.addEventListener(MouseEvent.CLICK, goRight);
function goLeft(e:MouseEvent):void {
buttonClicked = "left";
// change position
if (mcPosInc > 0 && mcMoving == false) {
mcMoving = true;
mcPosInc --;
boxAnimPlay --;
// Scroll mc1
mcLeft = new Tween(mc1, "x", Regular.easeOut, mc1.x, mc1.x + mcWidth, 1.5, true);
mcLeft.addEventListener(TweenEvent.MOTION_FINISH, end);
function end(event:TweenEvent) {
mcMoving = false;
}
if (boxAnimPlay >= 1) {
switch (boxAnimPlay) {
case 3:
mc1.box4.gotoAndPlay(17);
mc1.box3.gotoAndPlay(31);
break;
case 2:
mc1.box2.gotoAndPlay(31);
mc1.box3.gotoAndPlay(46);
break;
case 1:
mc1.box1.gotoAndPlay(17);
mc1.box2.gotoAndPlay(46);
break;
}
}
trace(boxAnimPlay);
trace(buttonClicked);
}
}
}
// Play movie tests
function goRight(e:MouseEvent):void {
buttonClicked = "right";
// change position
if (mcPosInc < 3 && mcMoving == false) {
mcMoving = true;
mcPosInc ++;
boxAnimPlay ++;
// Scroll mc1
mcRight = new Tween(mc1, "x", Regular.easeOut, mc1.x, mc1.x - mcWidth, 1.5, true);
mcRight.addEventListener(TweenEvent.MOTION_FINISH, end);
function end(event:TweenEvent) {
mcMoving = false;
}
if (boxAnimPlay <= 4) {
switch (boxAnimPlay) {
case 1:
break;
case 2:
mc1.box1.gotoAndPlay(1);
mc1.box2.gotoAndPlay(1);
break;
case 3:
mc1.box2.gotoAndPlay(16);
mc1.box3.gotoAndPlay(1);
break;
case 4:
mc1.box3.gotoAndPlay(16);
mc1.box4.gotoAndPlay(1);
break;
}
}
trace(boxAnimPlay);
trace(buttonClicked);
}
// Add Text Labels to TitleGroup
switch (boxAnimPlay) {
case 1:
readout.text = "test";
break;
}
}
I fixed it, basically the frames I was targeting in the gotoAndPlay contained stop() statements, once I targeted the subsequent frames, ie
Instead of:
switch (boxAnimPlay) {
case 3:
mc1.box4.gotoAndPlay(17);
mc1.box3.gotoAndPlay(31);
break;
case 2:
mc1.box2.gotoAndPlay(31);
mc1.box3.gotoAndPlay(46);
break;
case 1:
mc1.box1.gotoAndPlay(17);
mc1.box2.gotoAndPlay(46);
break;
}
I used this:
switch (boxAnimPlay) {
case 3:
mc1.box4.gotoAndPlay(17);
mc1.box3.gotoAndPlay(32); // no stop on subsequent frame
break;
case 2:
mc1.box2.gotoAndPlay(32); // no stop on subsequent frame
mc1.box3.gotoAndPlay(47); // no stop on subsequent frame
break;
case 1:
mc1.box1.gotoAndPlay(17);
mc1.box2.gotoAndPlay(47); // no stop on subsequent frame
break;
}

How can I press keyboard to play vdo only once

I'm here again for your help :{ I have a question which I tried to google it but can't find the answer. Well,..May be there is an answer but I just can't make it works? I'm in a process of learning AS3 so let's say I'm still new here.
What I'm doing is making a keyboatd to respond with the vdo files I have. It is a very simple idea as press-n-play. Each keys has their vdos to play and if you press another button while the first one is still pressing, it'll play another vdo of its key. I have make this as boolean with function of keydown and keyup like this:
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.net.NetStream;
import flash.net.NetConnection;
import flash.media.Video;
var isLeft:Boolean = false;
var isRight:Boolean = false;
var video;
var nc;
var ns;
stage.addEventListener(KeyboardEvent.KEY_DOWN,onDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onUP);
this.addEventListener(Event.ENTER_FRAME,playVid);
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.client = this;
video = new Video(550,400);
addChild(video);
video.attachNetStream(ns);
function onDown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37 :
//ns.play(TomAndJerry.flv);
isLeft=true;
break;
case 39 :
//ns.play(westler.flv);
isRight = true;
break;
}
}
function onUP(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37 :
isLeft = false;
break;
case 39 :
isRight = false;
break;
}
}
function playVid(e:Event):void
{
if (isLeft)
{
trace(kk);
ns.play(westler.flv);
isLeft = false;
}
else if (isRight)
{
trace(PP);
ns.play(TomAndJerry.flv);
//isRight = false;
}
}
I have tried making a keydown function without using any boolean or those true of false things to just play a vdo. It worked but, I still have the same problem that I can't find a solution which is ....
When you hold down the keyboard button the vdo will keep start at the beginning.
All I want is to play the vdo even the key is press down. If the vdo ends then play again as loop but if the key is up the vdo will play until it ends.
And if there are more than one button are holding down just play the vdo of the lastest pressed button.
T-T"
Thanks.
Ps. I have tried removeEventListener but, it made every buttons' function gone.
your playvid function is called each frame so i think it's normal your video don't start, I think you could try to change your code as follow:
// add net status handler event to check the end of the video
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
// remove this line
//this.addEventListener(Event.ENTER_FRAME,playVid);
/** a key is pressed **/
function onDown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.LEFT:
// start the video just if the video don't play
if(!isLeft) ns.play("TomAndJerry.flv");
// video left is playing
isLeft = true;
// video right isn't playing
isRight = false;
break;
case Keyboard.RIGHT:
// start the video just if the video don't play
if(!isRight) ns.play("westler.flv");
// video rightis playing
isRight = true;
// video left isn't playing
isLeft = false;
break;
}
}
/** a key is released **/
function onUP(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.LEFT:
isLeft = false;
break;
case Keyboard.RIGHT:
isRight = false;
break;
}
}
/** net status change, verify if we reach the end of the video **/
function netStatusHandler(e:NetStatusEvent):void
{
// when netStatus code is NetStream.Play.Stop the video is complete
if (e.info.code == "NetStream.Play.Stop")
{
// right key is still pressed we loop the video
if( isRight ) ns.play("westler.flv");
// left key is still pressed we loop the video
else if( isLeft ) ns.play("TomAndJerry.flv");
}
}
I hope this will help you :)

Continuos Timer and object for a game AS3

I am currently working on a game where you need to survive as long as possible while dodging questions that come your way. (all with AS3)
At the moment I am going from 1 scene to another in between the game field and the question field, but everytime I go to the question scene the timer in the game scene resets itself. I was wondering if it was possible to have the timer continue while being in the question scene?
Also I have a movable character in between the menus which incidentally are also made in different scenes and the player is able to move him around, and I would very much like him to stay in the last position he was in the next screen, as in I move him to the top right in the main menu and when I go to the options menu I want him to still be in the top right and not in his initial position.
As for my timer this is the code I am using at the moment:
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.globalization.DateTimeFormatter;
var timer:Timer = new Timer(100);
timer.start();
timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
var timerCount:int = 0;
function timerTickHandler(Event:TimerEvent):void
{
timerCount += 100;
toTimeCode(timerCount);
}
function toTimeCode(milliseconds:int) : void {
//create a date object using the elapsed milliseconds
var time:Date = new Date(milliseconds);
//define minutes/seconds/mseconds
var minutes:String = String(time.minutes);
var seconds:String = String(time.seconds);
var miliseconds:String = String(Math.round(time.milliseconds)/100);
//add zero if neccecary, for example: 2:3.5 becomes 02:03.5
minutes = (minutes.length != 2) ? '0'+minutes : minutes;
seconds = (seconds.length != 2) ? '0'+seconds : seconds;
//display elapsed time on in a textfield on stage
timer_txt.text = minutes + ":" + seconds+"." + miliseconds;
}
And my character is using this code:
/* Move with Keyboard Arrows
Allows the specified symbol instance to be moved with the keyboard arrows.
Instructions:
1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.
Note the number 5 appears four times in the code below.
*/
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
rutte.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
rutte.y -= 5;
}
if (downPressed)
{
rutte.y += 5;
}
if (leftPressed)
{
rutte.x -= 5;
rutte.scaleX = 1; // face left
}
if (rightPressed)
{
rutte.x += 5;
rutte.scaleX = -1; // face right
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
Thank you in advance for all the help you can give me.
Kind Regards.
in flash there is a fundamental aspect of how timeline and scenes works. Once you move away from a frame to another frame, The content of the frame and it's properties/states/actions are gone and reseted.
Personally I recommend you use a single scene with a timeline divided into frames labeled, there you can specify a layer for the actions and global variables, one for the user interface and another one for the specific actions of each frame. example:
So you never lose the reference of variables because the frame is not constantly recreated, all your important actions, including you timer, should be in your first frame.
I was testing, here you can see a working example: http://db.tt/FZuQVvt3. Here you can download the FLA file to be used as a base for your project: http://db.tt/RHG9G5lo

AS3 Loader() - loading multiple images with one loader

I am attempting to load multiple images in AS3 and I'm exploring different options on how this can be done.
I would like to have to only use one Load() instance and handle the various image assignments in the onComplete handler...
here is my first attempt:
var buttonLdr:Loader = new Loader();
buttonLdr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
buttonLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
whichButton = 0;
buttonLdr.load(new URLRequest("images/test.png"));
whichButton = 1;
buttonLdr.load(new URLRequest("images/testDown.png"));
whichButton = 2;
buttonLdr.load(new URLRequest("images/testHover.png"));
private function onLoadComplete(e:Event):void
{
switch(whichButton)
{
case 0:
buttonBgUp = e.target.loader.content;
break;
case 1:
buttonBgDown = e.target.loader.content;
break;
case 2:
buttonBgHover = e.target.loader.content;
break;
}
create();
}
A pretty straightforward approach... although it only calls the function on the last call, setting the buttonBgHover, but nothing else.
Any guidance to what might be going on here would be greatly appreciated.
-thanks
You need to call next load only when previous load completes.
whichButton = 0;
buttonLdr.load(new URLRequest("images/test.png"));
private function onLoadComplete(e:Event):void
{
switch(whichButton)
{
case 0:
buttonBgUp = e.target.loader.content;
whichButton = 1;
buttonLdr.load(new URLRequest("images/testDown.png"));
break;
case 1:
buttonBgDown = e.target.loader.content;
whichButton = 2;
buttonLdr.load(new URLRequest("images/testHover.png"));
break;
case 2:
buttonBgHover = e.target.loader.content;
break;
}
create();
}