okay so i got the guy to aim towards the mouse with this code. however i cant seen to find a way to set a max angle of 90 degrees to stop the guy from fully rotating when the mouse is beyong that point.
initially i got this which makes him turn with the keys and it works fine to stop him from rotating
Code:
maxAng=90;
function rotatePlayer(){
if (Key.isDown(Key.LEFT) && hero._rotation> -maxAng) {
hero._rotation -= 10;
}
if (Key.isDown(Key.RIGHT) && hero._rotation < maxAng) {
hero.gun._rotation += 10;
}
}
}
and this is the one am trying to get which is with the mouse and it doesnt work
Code:
var radians2:Number = 180/Math.PI;
var maxAng:Number = 89;
//Rotate Player
function rotatePlayer(){
playerX = player_mc._x;
playerY = player_mc._y;
//calculate player_mc rotation, based on player position & mouse position
rotationDirection =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
player_mc._rotation = rotationDirection;
trace(player_mc._rotation);
if (player_mc._rotation >= -maxAng) {
player_mc._rotation -= 5;
}if (player_mc._rotation <= maxAng) {
player_mc._rotation += 5;
updateAfterEvent();
}
}
edit:added the fla might help a little more. sorry is in flash cs3.
Last edited by mixedtrigeno; 08-07-2009 at 04:11 PM.
Project||[GAME]-on hold for now
------------------
[Hero]-80%
[Enemies]-1%
[Weapons]-90%
[Items]-0%
[Levels]-10%
the part thats not working is the one trying to stop the guy from making a full circle i want him to stop at 90 degrees. on the first code i got up there it works fine but thats with the keys. so this part of the code is not working
and yah am aware of the -180 and 180 thats why i got the -maxAng there
why not if (player_mc._rotation > maxAng) {
player_mc._rotation = maxAng;
} else if (player_mc._rotation < -maxAng) {
player_mc._rotation = -maxAng;
}
This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.
well, yes, I think. But also setting the value with an "=" instead of doing it the "+=" way is the most accurate. The other way if your angle is above your maxAng it will be decreasing by 5 every frame (or every certain time), which can look a bit sloppy sometimes. Instead, now you're saying: if the angle is above maxAng, it will be equal to maxAng, so no chance to go beyond that.
This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.
okay thanks didnt catch that one
now one more dilemma this is what i got so far.
i cant figure out at all how to adjust the rotating speed to give it a more natural feel to it. in other words i lets say you are on -maxAng and you move the mouse all of a sudden to maxAng i dont want him to get there at the same speed the mouse did i want him to rotate slower(rSpeed) hope am making sence.
Code:
var radians2:Number = 180/Math.PI;
var maxAng:Number = 89;
var rSpeed:Number =10;
var playerX = player_mc._x;
var playerY = player_mc._y
//Rotate Player
function rotatePlayer(){
//calculate player_mc rotation, based on player position & mouse position
player_mc._rotation =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
//player_mc._rotation = rotationDirection;
if (player_mc._rotation > maxAng) {
player_mc._rotation = maxAng;
} else if (player_mc._rotation < -maxAng) {
player_mc._rotation = -maxAng;
updateAfterEvent();
}
}
Project||[GAME]-on hold for now
------------------
[Hero]-80%
[Enemies]-1%
[Weapons]-90%
[Items]-0%
[Levels]-10%
This is a code I used to do what you are asking. It is in as1 though (cus I made it long ago), so you'll have to declare the variables. Also, the code is applied to a MC so adapt it to what you want. I just inserted your angle detecting code in there:
Code:
onClipEvent (enterFrame) {
//calculate player_mc rotation, based on player position & mouse position
rotation =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
rotminn = rotation-360;
rotmaxx = rotation+360;
_rotminn = _rotation-360;
_rotmaxx = _rotation+360;
sum1 = Math.abs(rotation-_rotation);
sum2 = Math.abs(rotminn-_rotation);
sum3 = Math.abs(rotmaxx-_rotation);
if (sum1< sum2 && sum1<sum3) {
if (rotation<_rotation-vel*2) {
_rotation-=vel;
} else if (rotation>_rotation+vel*2) {
_rotation+=vel;
}
} else if (sum2<sum1 && sum2< sum3) {
if (rotminn<_rotation-vel*2) {
_rotation-=vel;
} else if (rotminn>_rotation+vel*2) {
_rotation+=vel;
}
} else if (sum3<sum1 && sum3<sum2) {
if (rotmaxx<_rotation-vel*2) {
_rotation-=vel;
} else if (rotmaxx>_rotation+vel*2) {
_rotation+=vel;
}
}
}
onClipEvent (load) {
vel =3;
}
Now, if you want a more slight movement, rather than a constant speed, this is another way to do so:
Code:
onClipEvent (enterFrame) {
//calculate player_mc rotation, based on player position & mouse position
rotation =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
rotminn = rotation-360;
rotmaxx = rotation+360;
_rotminn = _rotation-360;
_rotmaxx = _rotation+360;
sum1 = Math.abs(rotation-_rotation);
sum2 = Math.abs(rotminn-_rotation);
sum3 = Math.abs(rotmaxx-_rotation);
if (sum1< sum2 && sum1<sum3) {
_rotation+=(rotation-_rotation)/7;
} else if (sum2<sum1 && sum2< sum3) {
_rotation+=(rotminn-_rotation)/7;
} else if (sum3<sum1 && sum3<sum2) {
_rotation+=(rotmaxx-_rotation)/7;
}
}
This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.
Notice that when working with rotation it can be a bit tricky because when it reaches 360 it should go back to 0, but I think Flash does it in a -180 to 180 way, so this way should work if you stay into that range.
This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.
okay this the code i ended up with after implenting one of your methods
Code:
var radians2:Number = 180/Math.PI;
var maxAng:Number = 89;
//this is the rotating speed that am trying to add
//var rSpeed:Number =10;
var playerX = player_mc._x;
var playerY = player_mc._y
//Rotate Player
function rotatePlayer(){
rotSpeed = 5;
//calculate player_mc rotation, based on player position & mouse position
rotDir=Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
player_mc._rotation=rotDir;
//player_mc cant rotate further 89 degrees when rotating left and/or right
if((player_mc._rotation=rotDir+rotSpeed)<(player._mc._rotation=rotDir)){
player_mc._rotation-=rotSpeed;
}else if((player_mc._rotation=rotDir-rotSpeed)>(player._mc._rotation=rotDir)){
player_mc._rotation+=rotSpeed;
}if (player_mc._rotation > maxAng) {
player_mc._rotation = maxAng;
}else if (player_mc._rotation < -maxAng) {
player_mc._rotation = -maxAng;
updateAfterEvent();
}
}
_root.onEnterFrame = function() {
rotatePlayer();
//function_AimAng();
}
i cant really explain what rotSpeed is doing, just test out the the fla ull see what i mean. this is a flash mx version
Project||[GAME]-on hold for now
------------------
[Hero]-80%
[Enemies]-1%
[Weapons]-90%
[Items]-0%
[Levels]-10%
function rotatePlayer(){
rotSpeed = 5;
//calculate player_mc rotation, based on player position & mouse position
rotDir=Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
//player_mc._rotation=rotDir;
//player_mc cant rotate further 89 degrees when rotating left and/or right
if((player_mc._rotation>rotDir+rotSpeed)){
player_mc._rotation-=rotSpeed;
}else if((player_mc._rotation<rotDir-rotSpeed)){
player_mc._rotation+=rotSpeed;
}if (player_mc._rotation > maxAng) {
player_mc._rotation = maxAng;
}else if (player_mc._rotation < -maxAng) {
player_mc._rotation = -maxAng;
updateAfterEvent();
}
}
First this line should not go there: player_mc._rotation=rotDir. With that you automatically make it not work, because you are setting the actual rotation (_rotation) to the desired rotation (rotDir) so there's no movement to do anymore.
Then in the if's you can't say something like this "(player_mc._rotation=rotDir+rotSpeed)" because, again you are setting the rotation to a specific value. Even when puttin it into an if, you cant place "=", you have to write "==" if you are checking a condition. In this case it's just < and > we are checking, no need of ==.
I'll try to explain it an easy way. The mc starts with a rotation of 0. Let's suppose the mouse is at an angle of 25. Then it says: the mouse angle (rotDir) is higher than the actual rotation (player_mc._rotation), then it will add +5 (rotSpeed) each frame until it gets to 25. That is basically what it does. I want to explain it well so you can modify it the way you want to fit your needs, I think I haven't explained very well.
So, you can change rotSpeed to any number you want, like 3 or other number depending on the speed you want. Also I noticed that the framerate is still at 12, that won't look so smooth so you'd better rise that a bit.
This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.
OMFG finally!!!!
and yah now i get understand the code
i owe you a million dollars when i win the lotto lol
and its only 12fps cuse i just made a quick fla for flash m the actual game is at 35 fps
edit: just noticed a big bug though...if you place the mouse behind the player_mc out of range of the maxAng the player_mc starts to rotate like crazy and if you move back withing the maxAng range it will rotate to the right...
oh and fyi i officially hate trigonometry after this lmao
Last edited by mixedtrigeno; 08-11-2009 at 12:18 AM.
Project||[GAME]-on hold for now
------------------
[Hero]-80%
[Enemies]-1%
[Weapons]-90%
[Items]-0%
[Levels]-10%
var rotation:Number = 0;
var rotminn:Number = 0;
var rotmaxx:Number = 0;
var _rotminn:Number = 0;
var _rotmaxx:Number = 0;
var sum1:Number = 0;
var sum2:Number = 0;
var sum3:Number = 0;
var vel:Number = 4;
function rotatePlayer() {
vel = 4;
yy = _root._ymouse - player_mc._y;
xx = _root._xmouse - player_mc._x
rotation = Math.atan2(yy, xx) * 180 / Math.PI;
rotminn = rotation-360;
rotmaxx = rotation+360;
_rotminn = player_mc._rotation-360;
_rotmaxx = player_mc._rotation+360;
sum1 = Math.abs(rotation-player_mc._rotation);
sum2 = Math.abs(rotminn-player_mc._rotation);
sum3 = Math.abs(rotmaxx-player_mc._rotation);
if (sum1< sum2 && sum1<sum3) {
if (rotation<player_mc._rotation-vel*2) {
player_mc._rotation-=vel
} else if (rotation>player_mc._rotation+vel*2) {
player_mc._rotation+=vel;
}
} else if (sum2<sum1 && sum2< sum3) {
if (rotminn<player_mc._rotation-vel*2) {
player_mc._rotation-=vel;
} else if (rotminn>player_mc._rotation+vel*2) {
player_mc._rotation+=vel;
}
} else if (sum3<sum1 && sum3<sum2) {
if (rotmaxx<player_mc._rotation-vel*2) {
player_mc._rotation-=vel;
} else if (rotmaxx>player_mc._rotation+vel*2) {
player_mc._rotation+=vel;
}
}
if (player_mc._rotation < -90) {
player_mc._rotation = -90;
} else if (player_mc._rotation > 90) {
player_mc._rotation = 90;
}
}
_root.onEnterFrame = function() {
rotatePlayer();
//function_AimAng();
}
I don't have a clue what was happening so I had to do it this way. Even doing it like this were weird things happening when I used the maxAng variable instead of putting the numbers directly into the if's. Don't know why. You'll just have to place your movieclip at rotation 0 by default, facing to the right and not like it is right now.
This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.
You just have to make your mc be facing to the right from inside. I mean, the rectangle shape that is inside the mc should be facing right (0 degrees) instead of facing upwards.
oh and fyi i officially hate trigonometry after this lmao
lol that's ironic, things like this were the ones that made me like this trigonometric stuff :P
This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.