[AS3] Auto-movement bug

Started by: Scorpioxxx | Replies: 4 | Views: 537

Scorpioxxx
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Oct 6, 2009 8:54 PM #496852
Right, so I sat and wrote this up, odd variable names are due to me having 3 or 4 files with similar variables to avoid overlap and ease copying between them.

If you test this with the correct instance names in AS3, you'll notice the player shakes about if one of it's co-ordinates fall in line and also once it gets to it's target location.

Script, notes are below to explain what's going on:

//--OUTLINE MOVE--//

stage.addEventListener( MouseEvent.CLICK, placeOutline );

function placeOutline( evt:Event ):void {

var z2 = mouseX;
var r2 = mouseY;

outline.x = z2;
outline.y = r2;
}

//END

//--RE-ROTATE--//

stage.addEventListener( Event.ENTER_FRAME, rotatePlayer2 );

function rotatePlayer2( evt:Event ):void {

var z3 = mouseX-player.x;
var r3 = mouseY-player.y;
var e3 = z3/r3;
var k3 = Math.atan (e3);
var g3 = k3*180/Math.PI;
var rotationplayer = g3 -=90;
if (mouseY>player.y) {
player.rotation = 90 - g3;
} else if (mouseY player.rotation=180-(g3-90);
}

}

//END

//--AUTOMOVE--//

stage.addEventListener( Event.ENTER_FRAME, movePlayer );

function movePlayer( evt:Event ):void {

var speed2 = 3;
var x1 = player.x;
var y1 = player.y;
var x2 = outline.x;
var y2 = outline.y;

if (x1>x2) {
player.x -= speed2;
} else {
if (x1 player.x += speed2;
}
}
if (y1>y2) {
player.y -= speed2;
} else {
if (y1 player.y += speed2;
}
}

}


The player, instance 'player', is set to face the mouse, this works fine and after removing this, it isn't cause the problem.

Icon, instance 'outline' is moved by clicking the mouse to the location clicked on.

The player checks it's x and y co-ords to see if it's in the right place, if it isn't, it fixes them. This is where the shaking occures and I want to eliminate this.

Help appreciated or an explanation of why it's doing this also appreciated.
CGIllusion
2

Posts: 617
Joined: Aug 2005
Rep: 21

View Profile
Oct 6, 2009 9:52 PM #496868
Can't really tell what's going on... I'm going to take a blind guess and say you should change x1>x2 to x1>=x2. Either that or turn speed to 1.

else if{
trace("do else if like this");
}

^^ you don't need all them extra brakkets n crap just makes things look ugly.
Scorpioxxx
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Oct 6, 2009 10:07 PM #496875
Quote from CGIllusion
Can't really tell what's going on... I'm going to take a blind guess and say you should change x1>x2 to x1>=x2. Either that or turn speed to 1.

else if{
trace("do else if like this");
}

^^ you don't need all them extra brakkets n crap just makes things look ugly.


Was else if {} in AS2? and i'll try that tomorrow I'm falling asleep here, changing it to 1 doesn't work though, just slows it down and makes smaller shaking.

Thanks.
CGIllusion
2

Posts: 617
Joined: Aug 2005
Rep: 21

View Profile
Oct 6, 2009 11:12 PM #496924
Iunno says as3 in the title.
Scorpioxxx
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Oct 7, 2009 9:55 PM #497369
Quote from CGIllusion
Iunno says as3 in the title.


I ment, i used AS2 and i never used else if {}

Btw, the adding an equals didn;t work but by dumb luck it lead me to fixing it, thanks mate.