PDA

View Full Version : [AS2] Script executes in Flash but not as Projector


spindrift
02-28-2010, 02:46 PM
Hey guys,

I'm using the syntax below for a Flash (CS3 and AS2) presentation so the client can play/stop and rewind/forward the movie with the keyboard.

It works perfectly when I go "Control >>> Test Movie" but when I publish the movie as a Projector file nothing happens when I hit the relevant keys.

Also, the same thing happens if I open the SWF outside of Flash from it's local folder.

Does anyone know why this could be happening?

Thank you and I hope to hear from you.

J





import mx.utils.Delegate;



var nCheckPoint:Number = 0;

var aCheckPoints:Array = new Array();

aCheckPoints.push( { sScene:"Scene 1", nFrame:10 } );

aCheckPoints.push( { sScene:"Scene 1", nFrame:27 } );

aCheckPoints.push( { sScene:"Scene 2", nFrame:54 } );

aCheckPoints.push( { sScene:"Scene 3", nFrame:17 } );



var oListener:Object = new Object();

oListener.onKeyDown = Delegate.create(this, onKeyDown);

Key.removeListener(oListener);

Key.addListener(oListener);



function onKeyDown():Void

{

switch(Key.getCode())

{

case Key.RIGHT:

nCheckPoint++

if(nCheckPoint >= aCheckPoints.length) {

nCheckPoint = 0;

}

GoToCheckPoint(nCheckPoint);

break;

case Key.LEFT:

nCheckPoint--;

if(nCheckPoint < 0) {

nCheckPoint = aCheckPoints.length - 1;

}

GoToCheckPoint(nCheckPoint);

break;

case Key.SPACE:

//Place your routine here maybe stop();

break;

default:

trace("UNHANDLED KEY");

break;

}

}



function GoToCheckPoint(nValue:Number):Void

{

var oChkPoint:Object = aCheckPoints[nValue];

var sScene:String = oChkPoint["sScene"];

var nFrame:String = oChkPoint["nFrame"];

gotoAndStop("sScene, nFrame");

}

spindrift
02-28-2010, 04:26 PM
right...i think when i was going to "control >>> test movie" i was just using the inbuilt flash timeline control keys (<, >, ENTER) - i thought it was the script! - how embarassing!

at the moment i just have the script in frame 1 of scene 1. does anyone know what i need to do in order to get this to work?

northcode
02-28-2010, 04:26 PM
I just distilled your code down to the basics and it seems to work fine. I've uploaded a copy of the FLA and the EXE to http://www.northcode.com/support/spindrift_key.zip in case you want to give it a try.

spindrift
02-28-2010, 04:36 PM
hi northcode,
thank you for the reply and help, thats fantastic!
do you know what i would need to do now in order to get it all to work?
thanks again,
j

northcode
02-28-2010, 05:04 PM
What's the problem? Does it work in scene 1 but not in the others?

spindrift
02-28-2010, 05:38 PM
thank you for the reply...

right, basically below is where im at with the code. ive got the arrow keys moving to points in the movie but it seems a bit erratic and only goes to some of the key points and not others. why could this be?

also, ive got the spacebar to perform a stop action. do you know how i can get it to play as well, like it can pause/play on command.




import mx.utils.Delegate;



var nCheckPoint:Number = -1;

var aCheckPoints:Array = new Array();

aCheckPoints.push( { sScene:"001", nFrame:69 } );

aCheckPoints.push( { sScene:"002_003", nFrame:34 } );

aCheckPoints.push( { sScene:"002_003", nFrame:137 } );

aCheckPoints.push( { sScene:"002_003", nFrame:240 } );

aCheckPoints.push( { sScene:"002_003", nFrame:344 } );

aCheckPoints.push( { sScene:"004_005", nFrame:80 } );

aCheckPoints.push( { sScene:"004_005", nFrame:191 } );

aCheckPoints.push( { sScene:"004_005", nFrame:281 } );



var oListener:Object = new Object();

oListener.onKeyDown = Delegate.create(this, onKeyDown);

Key.removeListener(oListener);

Key.addListener(oListener);



function onKeyDown():Void

{

switch(Key.getCode())

{

case Key.RIGHT:

nCheckPoint++

if(nCheckPoint >= aCheckPoints.length) {

nCheckPoint = 0;

}

GoToCheckPoint(nCheckPoint);

break;

case Key.LEFT:

nCheckPoint--;

if(nCheckPoint < 0) {

nCheckPoint = aCheckPoints.length - 1;

}

GoToCheckPoint(nCheckPoint);

break;

case Key.SPACE:

stop();

break;

case Key.SPACE:

play();

break;

default:

trace("UNHANDLED KEY");

break;

}

}


function GoToCheckPoint(nValue:Number):Void

{

var oChkPoint:Object = aCheckPoints[nValue];

var sScene:String = oChkPoint["sScene"];

var nFrame:Number = oChkPoint["nFrame"];

gotoAndPlay("sScene", nFrame);

}

northcode
03-01-2010, 02:59 AM
The first thing I see is

gotoAndPlay("sScene", nFrame);
should probably be...

gotoAndPlay(sScene, nFrame);

unless you have just one scene named "sScene".

In the first code you posted it was gotoAndStop("sScene, nFrame"); which is even more wrong (if that's possible).

spindrift
03-01-2010, 08:50 AM
hey,
thanks for the post and info.
when i insert gotoAndPlay(sScene, nFrame); flash wont execute the syntax as it says 'Scene name must be a quoted string', that's why I changed it.
im starting to think using scene labels would be much easier. do you know what i would have to do in order to get the code to read labels rather than scenes and frame numbers?
thank you,
j
heres where im at, at the moment:



import mx.utils.Delegate;



var nCheckPoint:Number = -1;

var aCheckPoints:Array = new Array();

aCheckPoints.push( { sScene:"001", nFrame:69 } );

aCheckPoints.push( { sScene:"002_003", nFrame:34 } );

aCheckPoints.push( { sScene:"002_003", nFrame:137 } );

aCheckPoints.push( { sScene:"002_003", nFrame:240 } );

aCheckPoints.push( { sScene:"002_003", nFrame:344 } );

aCheckPoints.push( { sScene:"004_005", nFrame:80 } );

aCheckPoints.push( { sScene:"004_005", nFrame:191 } );

aCheckPoints.push( { sScene:"004_005", nFrame:281 } );

aCheckPoints.push( { sScene:"006", nFrame:80 } );

aCheckPoints.push( { sScene:"007", nFrame:80 } );

aCheckPoints.push( { sScene:"007", nFrame:125 } );

aCheckPoints.push( { sScene:"007", nFrame:150 } );

aCheckPoints.push( { sScene:"007", nFrame:175 } );

aCheckPoints.push( { sScene:"007", nFrame:200 } );

aCheckPoints.push( { sScene:"007", nFrame:217 } );

aCheckPoints.push( { sScene:"007", nFrame:234 } );

aCheckPoints.push( { sScene:"007", nFrame:251 } );

aCheckPoints.push( { sScene:"007", nFrame:302 } );

aCheckPoints.push( { sScene:"007", nFrame:352 } );

aCheckPoints.push( { sScene:"007", nFrame:402 } );



var oListener:Object = new Object();

oListener.onKeyDown = Delegate.create(this, onKeyDown);

Key.removeListener(oListener);

Key.addListener(oListener);



function onKeyDown():Void

{

switch(Key.getCode())

{

case Key.RIGHT:

nCheckPoint++

if(nCheckPoint >= aCheckPoints.length) {

nCheckPoint = 0;

}

GoToCheckPoint(nCheckPoint);

break;

case Key.LEFT:

nCheckPoint--;

if(nCheckPoint < 0) {

nCheckPoint = aCheckPoints.length - 1;

}

GoToCheckPoint(nCheckPoint);

break;

case Key.SPACE:

play();

break;

case Key.ENTER:

stop();

break;

default:

trace("UNHANDLED KEY");

break;

}

}


function GoToCheckPoint(nValue:Number):Void

{

var oChkPoint:Object = aCheckPoints[nValue];

var sScene:String = oChkPoint["sScene"];

var nFrame:Number = oChkPoint["nFrame"];

gotoAndStop("sScene", nFrame);

}

northcode
03-01-2010, 01:21 PM
I've always avoided scenes so I didn't know that they had to be literals. To get this working as quickly as possible I would just move the "smarts" in the array into your GoToCheckPoint function. So you would keep using the index but get rid of the array and hard code the scene names in a bit switch statement in the GoToCheckPoint function.

spindrift
03-01-2010, 04:09 PM
thanks for that. as you have probably already noticed, im not exactly a flash wizard. could you explain in detail how to get it working please? thank you!

northcode
03-01-2010, 04:54 PM
You basically want to get rid of the array of objects you're setting up and move that information into the GoToCheckPoint function instead. Here's one way (see below).

You're still using a single index value to move from scene to scene but all the knowledge about what scene is associated with what index value is in the GoToCheckPoint function instead of in the array of objects.

The index value of the array is now the label used in the case statement.

It's not as elegant, but it will work ;)


function GoToCheckPoint(nValue:Number):Void
{
switch (nValue)
{
case 0: gotoAndStop("001", 69); break;
case 1: gotoAndStop("002_003", 34); break;
case 2: gotoAndStop("002_003", 137); break;

// etc.

default:
// uhhh... explode?
break;
}
}

spindrift
03-01-2010, 06:55 PM
wicked, thank you for all your help! absolute star!