is there any substitues for charCode for cs6? - actionscript-3

I am trying to us actionscript 3 to try and use a variable to play a separate animation but it doesn't work in flash cs6 and my school won't update it.
I have tried to use it in the context of a variable but it always spits out an error message:
var currentDirection = event.charCode;
Scene 1, Layer 'Sprite', Frame 1, Line 10 1120:Access of undefined property event

Perhaps what would help is a full example showing all the code needed in AS3 to accomplish this:
import flash.events.KeyboardEvent;
//var to hold the last key pressed.
var currentDirection:int = -1;
//listen for the key down event, when it happens, call the onKeyDown function below
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
//this function runs whenever a key goes down
function onKeyDown(event:KeyboardEvent) {
currentDirection = event.charCode; //assign the var the current key press char code
trace(event.charCode); //trace to the output panel to see if it's working
}

Related

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

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);
}
}

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

as3 error: access of possibaly undefind property through a reference with static type flash.display:DisplayObject

I have this as3 project, and in frame one of the timeline I tried to load a swf movie named "menu" and in this loaded movie I have an instance of a button named "button1", and I want to add a new EventListener to this "button1". my code is here:
var theLoader:Loader = new Loader();
var address:URLRequest = new URLRequest("menu.swf");
theLoader.load(address);
theLoader.contentLoaderInfo.addEventListener(Event.COMPLETE , swfDidLoad);
function swfDidLoad(evt:Event){
if(theLoader.content){
addChild(theLoader);
var button:SimpleButton = theLoader.content.button1;
button.addEventListener(MouseEvent.CLICK, handler1);
}
}
function handler1 (event:MouseEvent):void
{
removeChild(theLoader);
gotoAndStop(10);
};
but I get this undefind property error. what should I do? Am i doing this right at all?
The reason you are getting that error is because you are trying to access button1 on theLoader.content which is a non-dynamic DisplayObject (this means that only explicitly defined properties/methods are valid). You must first cast it to a MovieClip (which is dynamic).
You should change that line to:
var button:SimpleButton = MovieClip(theLoader.content).button1;

Modify stage size cause error - Flash CC

I have an application with 2 scene, the first scene contains 4 movieclips which are placed manually into the scene, every movieClip had an instance name (mc1,mc2..mc4), and I create an array with this objects var arr:Array = [mc1..mc4];
I add them an mouse event listener , for each (var i in arr){ i.addEvent...mouse.click)};
In this scene I also have an button "next scene" which have this code : nextScene();
In the second scene I have one button "back" which have this code : prevScene();
My app is 1200 x 720 px size, and I want it 800 x 600, so when I'm changing this manually .
When I'm running the application, everything is good, a go to next scene, and when I press back, it gives me an error at first scene
for each(var i in arr){
i.addEvent...mouse.click)
};
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/frame2()
at flash.display::MovieClip/prevScene()
at Main/onBack()
When I trace mc1 , at first run scene1 the output is //movieclip
when I'm pressing back the output is // null
and if I trace arr, the output is // ,movieclip,movieclip,movieclip (first only comma)
what can be the problem? thank you
Scene 1 code:
stop();
trace(mc1); // first run -> object [MovieClip]
// when back pressd -> null
var selectedIm:MovieClip = mc1;
var selectedD = d1;
var difficulty:uint = 3;
var imgs:Array = [mc1,mc2,mc3,mc4];
var diff:Array = [d1,d2,d3,d4];
goBtn.addEventListener(MouseEvent.CLICK, onGo);
for each(var i in imgs){
i.addEventListener(MouseEvent.CLICK, onImage); //here is the error, NULL OBJECT
}
function onGo(e:MouseEvent):void{ //next button
new Clk().play();
nextScene();
}
function onImage(e:MouseEvent):void{
new Clk().play();
if(selectedIm) selectedIm.filters = [];
selectedIm = e.target as MovieClip;
addOutline(selectedIm,0xFFFFFF,6);
}
...
Scene 2 code:
...
function onBack(e:MouseEvent):void{
new Clk().play();
removeChild(pz);
timer.reset();
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, numara);
prevScene();
....
}

Stop an Action Script at end of scene in Flash (AS3)

I am new to action script and i am having a few problems.
I have created a movie in Flash CC with 8 scenes. Scene 6 has an action script that randomly places birds which fly across the screen. Scene 7 has an almost identical script which does the same thing only slower (using a different symbol) and with a couple of variables changed.
The first problem surfaced when testing the movie with the action script only placed in scene 6, it simply did not end at the end of scene 6 and proceeded to play through scene 7 and scene 8.
When adding the script to scene 7, it breaks completely. Testing the individual scenes work fine but when testing the whole movie nothing happens and I get errors.
Here is the action script I am using:
Scene 6
import flash.utils.Timer;
var timer: Timer = new Timer(100, 45);
timer.addEventListener(TimerEvent.TIMER, timerF);
timer.start();
function timerF(event: TimerEvent): void {
var mcClip: Bird = new Bird();
var yVal:Number = (Math.ceil(Math.random()*200));
mcClip.x = -20;
mcClip.y = yVal;
addChildAt(mcClip,10);
}
Scene 7
import flash.utils.Timer;
var timer: Timer = new Timer(100, 40);
timer.addEventListener(TimerEvent.TIMER, timerF);
timer.start();
function timerF(event: TimerEvent): void {
var mcClip: Bird2 = new Bird2();
var yVal:Number = (Math.ceil(Math.random()*400));
mcClip.x = 0;
mcClip.y = yVal;
addChildAt(mcClip,3);
}
These are the errors I get:
Scene 7 (Birds), Layer 'Bird Action Script', Frame 1114, Line 3, Column 5
1151: A conflict exists with definition timer in namespace internal.
and
Scene 7 (Birds), Layer 'Bird Action Script', Frame 1114, Line 7, Column 10
1021: Duplicate function definition.
I need to stop the script at the end of each scene, but I don't know how. I have searched Google and cannot find anything helpful.
Any help would be greatly appreciated.
Many Thanks
Before scene 6 finishes you need to remove the Timer event listener, stop the timer and maybe even nullify it, ie:
timer.removeEventListener(TimerEvent.TIMER, timerF);
timer.stop();
timer = null;
If you're using the timeline, the error issues might be if the frame that 'contains' the actionscript for scene 6 might be carrying on into scene 7. Placing an empty keyframe on that layer before the end of the scene might help.
EDIT - in response to new comments below:
Before the line defining your timer, add:
var _birds:Array = new Array();
Then, in your TimerEvent handler:
function timerF(event: TimerEvent): void {
_birds.push(new Bird());
var yVal:Number = (Math.ceil(Math.random()*200));
_birds[_birds.length-1].x = -20;
_birds[_birds.length-1].y = yVal;
addChildAt(_birds[_birds.length-1], 10);
}
Then, at the end of your scene:
stop();
timer.removeEventListener(TimerEvent.TIMER, timerF);
timer.stop();
timer = null;
for (var loop:int=0;loop<_birds.length;loop++) {
removeChild(_birds[loop]);
}
_birds.length = 0;
// additional code here to go to next scene?
Obviously the timer variable names should change to match the names you're now using in each scene.
need to stop the script at the end of each scene
Simply place stop(); in the last frame of the scene.
Select last frame of the scene, hotkey F9 will open window with actions.