-
Sorry, I shouldn't have been hyjacking this post. I'm at work and I can't access the fla. Here's the fla though. I know what the problem is, I just don't know how to work around it. The problem is you have two max speeds, 1 for x and 1 for y. Until the x and y speed reaches their max speed it works fine but once they do then it will go strait in one direction. Either up down left right or up-right up-left down-right down-left. Once you reach your max speed there is no inbetween like there is in some space games. In other words it has 360 movement until max speed then it has only 8.
-
1 Attachment(s)
Sorry, I don't have flash 8 (I only have flash 6 / MX)
Is this the kind of motion you're after?
(set the "Den" box to 3 if you want 8 directions)
Once it reaches it's maxSpeed (of 10) it finds one of the 8 directions it's meant to go to, and moves towards the closest angle.
Changing "Den" changes the number of directions (num Dir = 2^Den, which is why I made 3 the default).
The general movement engine is the same as in that tutorial I was showing you guys. The main difference (which is what I think you're after) is the "splitter()" function I included. What is does is grabs the variable den (which is actually an exponent, not a denominator) and raises 2 to the power of it. This gives us the number of default directions we want. The rest of the script finds the angles that lie between these directions and changes the ship's rotation according to which direction is the closest to what it already is.
Code:
onClipEvent (load) {
thrust = 0.1;
decay = .99;
maxSpeed = 8;
//
//FUNCTION;
splitter = function (den) {
var rot = this._rotation+180;
if (den<=0) {
den = 1;
}
div = 360/(Math.pow(2,den+1));
for (var i = 0; i<(den*den); i++) {
if (rot>((i*2)*div) && rot<(((i*2)+1)*div)) {
this._rotation -= 1;
_root.delta_rot = "+";
}
if (rot>(((i*2)+1)*div) && rot<(((i*2)+2)*div)) {
this._rotation += 1;
_root.delta_rot = "-";
}
}
};
}
onClipEvent (enterFrame) {
deno = new Number(_root.denominator);
_root.rot = this._rotation;
if (Key.isDown(Key.RIGHT)) {
_rotation += 3;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 3;
}
if (Key.isDown(Key.UP)) {
xSpeed += thrust*Math.sin(_rotation*(Math.PI/180));
ySpeed += thrust*Math.cos(_rotation*(Math.PI/180));
flames._visible = 1;
} else {
xSpeed *= decay;
ySpeed *= decay;
flames._visible = 0;
}
speed = Math.sqrt((xSpeed*xSpeed)+(ySpeed*ySpeed));
if (speed>maxSpeed) {
splitter(deno);
xSpeed *= maxSpeed/speed;
ySpeed *= maxSpeed/speed;
}
_y -= ySpeed;
_x += xSpeed;
if (_y<0) {
_y = 400;
}
if (_y>400) {
_y = 0;
}
if (_x<0) {
_x = 400;
}
if (_x>400) {
_x = 0;
}
_root.note = "Note: AUTO-ROTATION kicks in at max speed and changes the"+newline+"rotation to point in 1 of "+Math.pow(2,deno)+" dynamically configured directions."
}
Please, please, PLEASE tell me this is what you're after...@_@;;
See the .fla file to see it in effect.
P.S. setting Den to 1 doesn't work properly, and anything higher than 3 is overkill...(4 makes 16 directions and 9 makes 512!!)
-
Yes it is!
With a little modification (taking off the auto rotate) it works My problem is it would move in one of 8 directions once hitting top speed. This takes care of that. Basically, if you are moving somewhere inbetween North and North-east and your facing that direction what would happen is after approaching top speed you would start moving north-east or north (which ever is closer) even if you aren't facing that direction exactly. Thanks :)
-
Here's the swf I now have.