Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Thread Tools Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
Old 03-23-2012, 08:53 PM   #1
Registered User
Join Date: Mar 2012
Posts: 1
Default isometric grid movement on mouse click with pathfinding bug

Hi I am currently working on a very simple isometric3d game with a hero to move to mouse click position, using path-finding. I am trying to code everything myself step by step,as a beginner , i am well aware of asolib3d but I don't want to use it...

I have read tutorials and few books I completely done with my isometric3d map. i create my path finding A-star class which is working perfectly with 2d grid tiles, but when i try to use it with isometric3d grid world it doesn't work properly and this is where i am asking your help.

it's not the best code but i am trying and sorry for my english.. ps: Patfhinding is very simple i use the wiki algorithm and it return an array of int.

ActionScript Code:
package  { import com.sayConcept.isometric.IsoUtils; import com.sayConcept.isometric.GraphicTile; import com.sayConcept.isometric.IsoWorld;    import com.sayConcept.isometric.Point3D; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.geom.Point; import flash.events.MouseEvent; import flash.filters.GradientBevelFilter; import flash.text.TextField; import fl.motion.MotionEvent; import flash.events.Event; [SWF(backgroundColor=0xffffff)] public class TheGame  extends Sprite{   private var world:IsoWorld;   [Embed(source="src/images/grass.png")]    private var grass:Class;    private var player:GraphicTile;      [Embed(source="src/images/grass1.png")]    private var grass1:Class;    private  var pos:Point3D;    private var index:int;     private var _walk:Array;     private var startx:int=0;     private var starty:int=0;     private var Newstartx:int;     private var Newstarty:int;     public function TheGame() {         stage.align = StageAlign.TOP_LEFT;         stage.scaleMode = StageScaleMode.NO_SCALE;          world=new IsoWorld();           world.x=stage.stageWidth/2;           world.y=100;           addChild(world);           for(var i:int=0; i<20; i++)           {                for(var j:int=0; j<20; j++)                {                     //creating my iso world..adding object grass                    var tile:GraphicTile=new GraphicTile(20,grass,20,10);                    tile.position=new Point3D(i*20,0,j*20);                    world.addChildToFloor(tile);                }           }        // stage.addEventListener(MouseEvent.CLICK,addNewTile);      if( Newstartx==0 &&  Newstarty==0)         {         addPlayer(startx,starty);         }         else         {             addPlayer(Newstartx,Newstarty);                      }      stage.addEventListener(MouseEvent.CLICK,onEnterFrame1);     }     private function  addPlayer(startx:Number, starty:Number):void     {         player=new GraphicTile(20,grass1,20,20);         var pos:Point3D=IsoUtils.screenToIso(new Point(startx, starty));          pos.x=Math.round(pos.x/20)*20;         pos.y=Math.round(pos.y/20)*20;         pos.z=Math.round(pos.z/20)*20;         player.position=pos;         world.addChildToWorld(player);     }     private function onEnterFrame1(e:Event):void     {                  trace("mouse"+world.mouseX+","+world.mouseY);                       pos =IsoUtils.screenToIso(new Point(world.mouseX,world.mouseY));                  var targetX:Number = Math.round(pos.x/20)*20;                  var targetY:Number =Math.round(pos.y/20)*20;                  var targetZ:Number =Math.round(pos.z/20)*20;                  var path1:Pathfinding =new Pathfinding();                                   _walk=new Array();       //check if =to zero       if( Newstartx==0 &&  Newstarty==0 ||  Newstartx==undefined && Newstarty==undefined)         {            startx=Newstartx;                        starty=Newstarty;         }         else         {             //new position for hero => new reach goal             startx=Newstartx;                        starty=Newstarty;         }                 //call to pathfinding                 if( path1.Path(startx/20, starty/20,targetX/30,targetZ/20))                 {                     addEventListener(Event.ENTER_FRAME,loop, false, 0, true);                  //_walk=path1.Path(player.position.x/20, player.position.z/20,targetX/40,targetZ/40);                 _walk=path1.Path(startx/20, starty/20,targetX/20,targetZ/20);                   Newstartx=targetX;                   Newstarty= targetZ;                 }     }     ////looop     private function loop(e:Event):void     {                     var pathX:Number = _walk[index].x * 20 + 20/ 2;                     var pathY:Number = _walk[index].y * 20 + 20 / 2;                     var dx:Number = pathX - player.x;                     var dy:Number = pathY - player.y;                     var temp:Number = Math.sqrt(dx * dx + dy * dy);                     if(temp < 1)                     {                         index++;                         if(index >= _walk.length)                         {                             index=0;                             startx=_walk[index].x * 20 + 20 / 2;                                starty=_walk[index].y * 20 + 20 / 2;                             removeEventListener(Event.ENTER_FRAME, loop);                         }                     }                     else                     {                         player.x += dx * .3;                         player.y += dy * .3;                         ////show path on the nodes grid                         graphics.beginFill(0x660000);                         graphics.drawCircle(player.x ,player.y *20, 4);                         graphics.endFill();                     }     } } }

Let me explain, my character movement is not correct. If hero position=0,0 and I click 0,10 on my iso grid instead of moving down he will move on the right side. If I am in the middle and I click on the left he will move right. and like few steps for where it supposes to stop... my pathfinding is ok in 2d but isometric the movement is weird...

Can anyone explain to me how I could fix this, or point me to any examples or tutorials. I just want my hero to move to the path correctly...thank you.

3rdnubas3 is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 10:05 PM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.