Page 1 of 1

Tut Request: Character moving

PostPosted: Sun Sep 04, 2011 2:35 pm
by Nikag
Hi,

I'm hust a beginner with flash, and I can't realy understand on how to move the character around the map smoothly. (hero1 is a ball).

I used to move all the scene:

Code: Select All Code
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkArrow3);

stop();
function checkArrow3(e:KeyboardEvent):void
{if ((e.keyCode==37)) {
this.x+=5;
hero1.x-=5;            }
            else if ((e.keyCode==39)) {
this.x-=5;
hero1.x+=5;            }
}


and I used

Code: Select All Code
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkArrow3);

stop();

var rect:Rectangle;
rect = new Rectangle(0,0, 1024, 768);

this.scrollRect = rect;

stop();
function checkArrow3(e:KeyboardEvent):void
{
 if ((e.keyCode==37)) {
rect.x-=10;
hero1.x-=10;
this.scrollRect = rect;
            }
            else if ((e.keyCode==39)) {
rect.x+=10;
this.scrollRect = rect;
hero1.x+=10;}


but with no difference. What i want is to make it moving on the scene smoothly like it's here: viewtopic.php?f=28&t=1623

Re: Tut Request: Character moving

PostPosted: Sun Sep 04, 2011 10:34 pm
by Nikag
As I've found it's a well known bug of AS3, somehow connected to processing KeyDown command. For the moment the only solution to make it mooving smoothly I've found is to use timer-based functions:

Code: Select All Code
var timer:Timer;
var direct:String;
initStage();

function initStage() {
   stage.addEventListener(KeyboardEvent.KEY_DOWN,startMove);
}

function startMove(e:KeyboardEvent):void {
   switch (e.keyCode) {
      case Keyboard.RIGHT:
      direct = "right";
      break;
      case Keyboard.LEFT:
      direct = "left";
      break;     
      case Keyboard.DOWN:
      direct = "down";
      break;
      case Keyboard.UP:
      direct = "up";
   }
   timer = new Timer(10);
   timer.addEventListener(TimerEvent.TIMER, moveCar);
   timer.start();
   stage.removeEventListener(KeyboardEvent.KEY_DOWN, startMove);
   stage.addEventListener(KeyboardEvent.KEY_UP, stopMove);
}

function stopMove(e:KeyboardEvent):void {
   timer.stop();
   initStage();
}

function moveCar(e:TimerEvent):void {
   switch (direct) {
      case "right":
      this.x -= 5;
     hero1.x+=5;
      break;
      case "left":
      this.x += 5;
     hero1.x-=5;
      break;
      case "up":
      this.y -= 5;
      break;
      case "down":
      this.y += 5;
      break;     
   }
}


Please leave the unswers if you know better solution.
For now it looks like here:

Re: Tut Request: Character moving

PostPosted: Mon Sep 05, 2011 2:12 am
by OwnerOfSuccuby
Try to test is button pressed each period of time. If it is move object. For example this period of time can be timer (but it lags some time so i do not like to use him) - or for example it can be event of frame goes to another frame (in object for example).

I made this in that way:

Code: Select All Code
  //What to do if key is pressed

  function myPlayerMovier(e:KeyboardEvent):void{
    if (MoveType=="CRun"){
      switch (e.keyCode) {
        case myKeyKodeLeft :
          moveLeft=1;
        moveLast=-1;
        break;
        case myKeyKodeUp :
          moveUp=1;
        break;
       case myKeyKodeRight :
          moveRight=1;
        moveLast=1;
        break;
        case myKeyKodeDown :
          moveDown=1;
        break;
        case myKeyKodeJump :
         if (moveJump==0){
          JumpCounter=0;
            moveJump=1;
        }
        if ((StoitNaDiravomPoly==true)&&(PlayerPositionToPlay==3)){
         JumpCounter=cJumpHeight;
         Player1.y+=JumpDownFromFloor;
        }
        break;
        case myKeyKodeFire :
            moveFire=1;
          break;
 /*       default :
          trace("some other key"); */
      }
    }//if CRun
  }

  function myPlayerUnMovier(e:KeyboardEvent):void{
    if (MoveType=="CRun"){
      switch (e.keyCode) {
        case myKeyKodeLeft :
          moveLeft=0;
        break;
        case myKeyKodeUp :
          moveUp=0;
        break;
       case myKeyKodeRight :
          moveRight=0;
        break;
        case myKeyKodeDown :
          moveDown=0;
        break;
        case myKeyKodeJump :

        //if (moveJump==1) {JumpCounter=cJumpHeight;} //If you need to do controling jump that let player go down after jump button is unlocked remove //
        break;
       case myKeyKodeFire :
          moveFire=0;
        FireIntensityCounter=0;
        break;
    /*    default :
          trace("some other key"); */
      }
    }//if CRun
  }
}


This was one of my first versions of game motion algoritm (now they are much more better - but they are big in sixe =() and this one is very small and simple.

Re: Tut Request: Character moving

PostPosted: Mon Sep 05, 2011 9:28 am
by GolanTrevize
Have you tried adding more frames per second (20-30 would be great)?

Re: Tut Request: Character moving

PostPosted: Mon Sep 05, 2011 9:37 am
by OwnerOfSuccuby
I always use 24 or 12 fps. If you try to move very big objects it is possible that it will start lag then probably than bigger is an object (in megabites :mrgreen: ). And i heard that humans better see movies like 24 frame per second (and 12 for some kind of animation like Anime) i do not know is it true or not but i think it is :mrgreen: .

Re: Tut Request: Character moving

PostPosted: Mon Sep 05, 2011 3:06 pm
by KaTsuO_O
Quote Nikag:

Code: Select All Code
var timer:Timer;
var direct:String;
initStage();

function initStage() {
   stage.addEventListener(KeyboardEvent.KEY_DOWN,startMove);
}

function startMove(e:KeyboardEvent):void {
   switch (e.keyCode) {
      case Keyboard.RIGHT:
      direct = "right";
      break;
      case Keyboard.LEFT:
      direct = "left";
      break;     
      case Keyboard.DOWN:
      direct = "down";
      break;
      case Keyboard.UP:
      direct = "up";
   }
   timer = new Timer(10);
   timer.addEventListener(TimerEvent.TIMER, moveCar);
   timer.start();
   stage.removeEventListener(KeyboardEvent.KEY_DOWN, startMove);
   stage.addEventListener(KeyboardEvent.KEY_UP, stopMove);
}

function stopMove(e:KeyboardEvent):void {
   timer.stop();
   initStage();
}

function moveCar(e:TimerEvent):void {
   switch (direct) {
      case "right":
      this.x -= 5;
     hero1.x+=5;
      break;
      case "left":
      this.x += 5;
     hero1.x-=5;
      break;
      case "up":
      this.y -= 5;
      break;
      case "down":
      this.y += 5;
      break;     
   }
}


... Why so much code, it is just ridiculous. My (experimental) solution for what you got is this:

Click to Play
(Javascript Required)

Rolling ball.swf [ 1.75 KiB | Viewed 2131 times ]



This is the code that is used in the ball (AS2) and it is also the only piece of code that is used in the document.

Code: Select All Code
onClipEvent (enterFrame) {
   speed = 3;
}
onClipEvent (enterFrame) {
if (Key.isDown (Key.RIGHT)) {
   _root.ball._x += speed;
   _root.ball._rotation += speed;
}
if (Key.isDown (Key.LEFT)) {
   _root.ball._x -= speed;
   _root.ball._rotation -= speed;
}
if (Key.isDown (Key.RIGHT) && Key.isDown (Key.SHIFT)) {
   _root.ball._x += speed*1.4;
   _root.ball._rotation += speed*1.4;
}
if (Key.isDown (Key.LEFT) && Key.isDown (Key.SHIFT)) {
   _root.ball._x -= speed*1.4;
   _root.ball._rotation -= speed*1.4;
}
}


The first half of the code makes me able to move the ball and the other half makes me able to speed it up if I hold down shift so that means even if i add a few more things to it, it still is less code. I don't know if it is AS3 that makes things so much more complicated or if it just is the way you made it.

Also, you can press any key on the keyboard (I believe) and it moves in the same direction as you pressed.

Re: Tut Request: Character moving

PostPosted: Mon Sep 05, 2011 4:11 pm
by Nikag
Thanks for answers, KaTsuO_O, but at AS3 this also moves objects, but looks very very bad (it is used at first flash at the top of topic)

Great thx to OwnerOfSuccuby, I hope I can handle with your coding. And by the way, ты русский? :lol:

Re: Tut Request: Character moving

PostPosted: Mon Sep 05, 2011 7:07 pm
by OwnerOfSuccuby
Увы :lol: Да я русский :lol: :mrgreen:

I am not sure but i do not know how to use key down on as3. I like this method to but i can not understand how to use it in as3 so i had to go another way. Check is it pressed and was it released. I do not like this method but it works :mrgreen:

I check key godes of pressed and released buttons using this: