Tut Request: Character moving

A place for tutorials on how to get the most out of Flash

Tut Request: Character moving

Postby Nikag » Sun Sep 04, 2011 2:35 pm

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
Attachments
Click to Play
(Javascript Required)

AS3.swf [ 38.81 KiB | Viewed 2195 times ]

User avatar
Nikag
 
Joined: Sun Aug 28, 2011 11:59 pm

Re: Tut Request: Character moving

Postby Nikag » Sun Sep 04, 2011 10:34 pm

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:
Attachments
Click to Play
(Javascript Required)

AS3.swf [ 39.23 KiB | Viewed 2159 times ]

User avatar
Nikag
 
Joined: Sun Aug 28, 2011 11:59 pm

Re: Tut Request: Character moving

Postby OwnerOfSuccuby » Mon Sep 05, 2011 2:12 am

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.
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Tut Request: Character moving

Postby GolanTrevize » Mon Sep 05, 2011 9:28 am

Have you tried adding more frames per second (20-30 would be great)?
Current project: Dark moon (Artists needed)
User avatar
GolanTrevize
 
Joined: Wed Jul 20, 2011 3:32 pm

Re: Tut Request: Character moving

Postby OwnerOfSuccuby » Mon Sep 05, 2011 9:37 am

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: .
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Tut Request: Character moving

Postby KaTsuO_O » Mon Sep 05, 2011 3:06 pm

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 2127 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.
Don't create a porn game if you're only interested in porn.
Wise words regarding criticism http://www.youtube.com/watch?v=-98ZFl1sKt4
User avatar
KaTsuO_O
 
Joined: Tue Nov 02, 2010 6:03 pm

Re: Tut Request: Character moving

Postby Nikag » Mon Sep 05, 2011 4:11 pm

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:
User avatar
Nikag
 
Joined: Sun Aug 28, 2011 11:59 pm

Re: Tut Request: Character moving

Postby OwnerOfSuccuby » Mon Sep 05, 2011 7:07 pm

Увы :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:
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm


Return to Tutorials



Who is online

Users browsing this forum: Bing [Bot]