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