5 Replies Latest reply on Apr 18, 2017 9:12 AM by kglad

    Walk a character using mouse event AS3

    cjyoon1227

      Hello everyone! I'm creating an Android game for our thesis. I want to walk my character after I click the stage (in one direction only), and the character goes to the key. I've searched about this thing on an internet then I found a code about this but it fails. I really don't know what code that I can put on an Actions (F9), I've tried and tried but until now, it fails.

       

      My character has 9 keyframes on it, one movement per keyframe:

       

      Here's the picture of my actual game interface (with edited texts):

      Note: The long arrow shows that I want my character to go thru the key after clicking a stage.

       

      And I don't think that my frames are right because I did two keyframes per game because after I got a key, the questionnaire will appear and after the "correct!" window appears, the door will open and the character will disappear after it enters to the door.

       

      And here's my code:

      stop();

       

       

      import flash.text.*;

      import flash.display.MovieClip;

      import flash.net.URLLoader;

      import flash.net.URLRequest;

      import flash.events.MouseEvent;

      import flash.events.Event;

      import flash.events.*;

      import flash.display.DisplayObject;

      import flash.geom.Point;

      import flash.display.Stage;

      import flash.display.Sprite;

       

       

      var buhay = 3;

      var iskoru = 0;

      var hinto = 3;

      var iswalking = false;

      var goX = playergril.x; // goX - the point on the X axis to which playergril must move

      var goY = playergril.y; // same as goX but on the Y axis

      var movespeed = 5; // movespeed of our hero

      var dir = "step1"; // current direction of movement

      var keyCollected: Boolean = false;

      var doorOpen: Boolean = false;

      var limiterX = key.x;

      var limiterY = key.y;

      var min_x = 10;

      var min_y = 5;

      var max_x = 20;

      var max_y = 10;

       

       

      stage.addEventListener(Event.ENTER_FRAME, loop);

       

       

      function loop(Event) {

        // animation handling

       

       

        // direction handling

       

       

        playergril.gotoAndStop(dir);

       

       

        // movement handling

       

       

        if ((goX - movespeed) > playergril.x) {

        playergril.x += movespeed;

        if(playergril.x > max_x){

        playergril.x = max_x;

        dir = "step1";

        iswalking = true;

        }

        } else if ((goX + movespeed) < playergril.x) {

        playergril.x -= movespeed;

        if(playergril.x > min_x){

        playergril.x = min_x;

        dir = "step3";

        iswalking = true;

        }

        } else if ((goY - movespeed) > playergril.y) {

        playergril.y += movespeed;

        if(playergril.y > max_y){

        playergril.y = max_y;

        dir = "step6";

        iswalking = true;

        }

        } else if ((goY + movespeed) < playergril.y) {

        playergril.y -= movespeed;

        if(playergril.y > min_y){

        playergril.y = min_y;

        dir = "step9";

        iswalking = true;

        }

        } else {

        iswalking = false;

        }

       

       

      }

       

       

      stage.addEventListener(MouseEvent.CLICK, setposition);

       

       

      // set the position to which playergril must move on click

       

       

      function setposition(MouseEvent) {

        goX = mouseX;

        goY = mouseY;

      }

       

       

      if (keyCollected == false) {

        if (playergril.hitTestObject(key)) {

        key.visible = false;

        keyCollected = true;

        trace("key collected");

        }

      }

      btnForward.addEventListener(MouseEvent.CLICK, forward);

      function forward(event: MouseEvent): void {

        gotoAndStop(76);

        playergril.visible = false;

      }

       

      Is there something wrong on that code? Any suggestions or code that may solve on my problem? Any help will be appreciated. Thanks!