jeudi 23 juin 2016

Actionscript 3.0 random teleportation

I'm using Flash and decided to make my animation into a game of a sort. So I started out coding and I'm pretty far into it. But I came across a small problem when I went pressed my chosen key and it did what i wanted, but randomly teleported to the characters original location.

//FRAME 1
stop();
//--Developer William Richmond

import flash.events.KeyboardEvent;
import flash.events.TimerEvent;

stage.addEventListener(KeyboardEvent.KEY_UP,moveClipdown);
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveClipup);
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveClipright);
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveClipleft);


//Attempting the border restriction code
function limitStageBorder(object:MovieClip) {
//1.
var objectHalfWidth:uint=object.width/2;
var objectHalfHeight:uint=object.height/2;

//2.
if (object.x+objectHalfWidth>stage.stageWidth) {
   object.x=stage.stageWidth-objectHalfWidth;
}
else if (object.x -  objectHalfWidth <0) {        
   object.x=0+objectHalfWidth;    
}     

//3.    
if (object.y-objectHalfHeight<0) {        
   object.y=0+objectHalfHeight;    
}    
else if (object.y + objectHalfHeight > stage.stageHeight) {
   object.y=stage.stageHeight-objectHalfHeight;
}
}
//down
function moveClipdown(event:KeyboardEvent):void
              {
                  if(event.keyCode == 83)
                  {
                      mcSquare1.y += 25
                  }
              }
//up
function moveClipup(event:KeyboardEvent):void
              {
                  if(event.keyCode == 32)
                  {
                      mcSquare1.y -= 10
                      mcSquare1.gotoAndPlay(20);
                  }
              }
            //right  
function moveClipright(event:KeyboardEvent):void{
                  if(event.keyCode == 68){
                      trace("I am running to the right");
                      mcSquare1.scaleX = 0.75;
                      mcSquare1.x += 20
                      gotoAndStop(10);
                  }
}

            //Left  
function moveClipleft(event:KeyboardEvent):void{
                  if(event.keyCode == 65)
                  {
                      trace("I am running to the left");
                      mcSquare1.scaleX = -0.75;
                      mcSquare1.x -= 20
                      gotoAndStop(10);
                  }
              }

FRAME 10

stop();
//--Developer William Richmond

import flash.events.KeyboardEvent;
import flash.events.TimerEvent;

stage.addEventListener(KeyboardEvent.KEY_UP,moveClipright10);
stage.addEventListener(KeyboardEvent.KEY_UP,moveClipleft10);

            //right  
function moveClipright10(event:KeyboardEvent):void{
                  if(event.keyCode != 68){
                      trace("I am standing");
                      mcSquare1.scaleX = 0.75;
                      mcSquare1.x += 20
                      gotoAndStop(1);
                  }
}

            //Left  
function moveClipleft10(event:KeyboardEvent):void{
                  if(event.keyCode != 65)
                  {
                      trace("I am standing");
                      mcSquare1.scaleX = -0.75;
                      mcSquare1.x -= 20
                      gotoAndStop(1);
                  }
              }

So if anyone could have a go at helping out, it would be highly appreciated!

Aucun commentaire:

Enregistrer un commentaire