Sunday, 8 February 2015

CASE SWITCH BREAK


btn_rainbow.addEventListener(MouseEvent.CLICK, clickHandler);
btn_tecfa.addEventListener(MouseEvent.CLICK, clickHandler);
btn_bosses.addEventListener(MouseEvent.CLICK, clickHandler);
btn_my_computers.addEventListener(MouseEvent.CLICK, clickHandler);
btn_credits.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event:MouseEvent):void {
switch (event.currentTarget.label){
case "Rainbow" :
gotoAndStop(2);
break;
case "TECFA" :
gotoAndStop(3);
break;
case "Bosses" :
gotoAndStop(4);
break;
case "My computers" :
gotoAndStop(5);
break;
case "Credits" :
gotoAndStop(6);
break;
}}

------------------------------------------------------------------------------------------------------------------------
MOVIE SYMBOL MOVEMENT ARROW KEYS AS3
https://www.youtube.com/watch?v=wQo-IDuT8qo
------------------------------------------------------------------------------------------------------------------------

//MAKE MOVIE SYMBOL ,GIVE INSTANCE NAME " my_mc "
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onDown)
var my_mc:MovieClip;
function onDown(event:KeyboardEvent) : void{
trace(event.keyCode);
switch(event.keyCode){
case Keyboard.LEFT:{
this.my_mc.x = this.my_mc.x - 5;
break;
}
case Keyboard.RIGHT:{
this.my_mc.x = this.my_mc.x + 5;
break;
}
case Keyboard.UP:{
this.my_mc.y = this.my_mc.y - 5;
break;
}
case Keyboard.DOWN:{
this.my_mc.y = this.my_mc.y + 5;
break;
}}}


Arrow Key Movement AS2

Arrow key movement is very simple in AS2. All that needs to be done is calling the Key.isDown() function.

AS2 Code

//myExample is just a placeholder 
//when coding you can replace myExample with your own instance name
 
onClipEvent(enterFrame){
   
   if(Key.isDown(Key.RIGHT)){ //checks if the right arrow key is being pressed down
   myExample._x += 5;         //adds 5 to the x value of the shape
   }
 
   if(Key.isDown(Key.LEFT)){  //checks if the left arrow key is being pressed down
   myExample._x -= 5;         //subtracts 5 to the x value of the shape
   }
 
   if(Key.isDown(Key.UP)){    //checks if the up arrow key is being pressed down
   myExample._y -= 5;         //subtracts 5 to the y value of the shape
   }
   
   if(Key.isDown(Key.DOWN)){  //checks if the down arrow key is being pressed down
   myExample._y += 5;         //adds 5 to the y value of the shape
   }
}










Arrow Key Movement AS3

Arrow key movement in AS3 is much more complex. You have to use a few event listeners and add a few import statements.

AS3 Code

// add these import statements
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
 
// add the event listeners
myExample.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
 
// this array holds references to all the keys
var keys:Array = [];
 
// the event listeners
function update(e:Event):void
{
 if (keys[Keyboard.RIGHT])
      {
          myExample.x += 5;
      }
 
 if (keys[Keyboard.LEFT])
 {
  myExample.x -= 5;
 }
 if (keys[Keyboard.UP]) 
 {
  myExample.y -= 5;
 }
 if (keys[Keyboard.DOWN])
 {
  myExample.y += 5;
 }
}
 
function onKeyDown(e:KeyboardEvent):void
{
 keys[e.keyCode] = true;
}
 
function onKeyUp(e:KeyboardEvent):void
{
 keys[e.keyCode] = false;
}

Final Notes

------------------------------------------------------------------------------------------------------------------------


http://www.gotoandplay.it/_articles/2007/02/game_tutorial_part1.php?PHPSESSID=ec82faadbef6cd6671cca99a4c0e6ab8

Well, you will notice that our hero always moves at a fixed speed.
I want to set up a variable to store the speed, so if I want the hero to move faster/slower I only have to change one value.
This is very important since I am planning to develop different levels where hero speed may vary.
I need to set up a "power" variable to move the hero fastly or slowly.
onClipEvent (load) {
    power = 3;
}
onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
        _x -= power;
    }
    if (Key.isDown(Key.RIGHT)) {
        _x += power;
   }
   if (Key.isDown(Key.UP)) {
       _y -= power;
   }
   if (Key.isDown(Key.DOWN)) {
       _y += power;
   }
}
    
//----------------------------------------------------------------------
//Instance of Symbol 2 MovieClip [hero] "hero" in Frame 1
//----------------------------------------------------------------------
onClipEvent (load) {
power = 3;
}
onClipEvent (enterFrame) {
if (Key.isDown(37)) {
_x = (_x - power);
}
if (Key.isDown(39)) {
_x = (_x + power);
}
if (Key.isDown(38)) {
_y = (_y - power);
}
if (Key.isDown(40)) {
_y = (_y + power);
}
}

//----------------------------------------------------------------------
//Symbol 7 Button
//----------------------------------------------------------------------
on (release) {
_root.hero._x = 231;
_root.hero._y = 20;
}







READ MORE:
------------------------------------------------------------------------------------------------------------------
https://actionscriptpro.wordpress.com/
http://edutechwiki.unige.ch/en/Flash_component_button_tutorial
http://flashgamedojo.com/wiki/index.php?title=Arrow_Key_Movement_(Actionscript)#Arrow_Key_Movement_AS2
http://tecfa.unige.ch/guides/flash/ex6/components-intro/flash-cs6-simple-slide-show-menu.html





EmoticonEmoticon