PDA

View Full Version : tank shooting/moving



danulf
February 16th, 2005, 06:24 PM
ok! So I have a rotating barrel (or what it's called), and the basic for the shooting. But I can't get the tank to move, because then the barrel wont rotate/shoot. The barrel and the body of the tank are seperate MC's.
My question: How do I make the tank move at the same time as you can move the barrel?

Then I have a few more questions, but this is the most urgent.

Hope you understand what I am trying to say :D

Marz
February 16th, 2005, 07:37 PM
Instead of using else if's to test your keyPresses... I would suggest using on if statements.




_root.onEnterFrame = function()
{
if(Key.isDown(Key.DOWN))
{
move tank down;
}
if(Key.isDown(Key.UP))
{
move tank up;
}
if(Key.isDown(Key.LEFT))
{
move tank left;
}
if(Key.isDown(Key.RIGHT))
{
move tank right;
}
if(Key.isDown(Key.SPACE))
{
have tank shoot;
}
// and so on and so forth....
}

danulf
February 17th, 2005, 03:35 AM
yeah, I've tried that, but I want the barrel to move after the mouse.

Marz
February 17th, 2005, 04:42 AM
And your point is? As long as you have an if statement with the arrow keys, putting in a mouse location and pointing the barrel towards the mouse is easy.

._xmouse and ._ymouse is the location of your mouse's pointer on the screen. Use those along qwith trigonometry and put the results in the onEnterFrame. And you'll be fine.

danulf
February 17th, 2005, 06:17 AM
Yes... But I can't make the tank move at the same time as the barrel is moving...
The barrel is one MC and the tank is one. How do I attach them to eachother so that the mouse pointing thing wticks with the tank.... It's hard to explain, when I get home, I can post the .fla

Marz
February 17th, 2005, 06:51 AM
Simple enough.... The base of each MC willl stay together. Your barrel should pivot around one point and that point should be the x and y coordinates of your tank + or - some pixels here and there. So everytime the player moves the tank, he also moves the turret.



// Case in point

if(Key.isDown(Key.DOWN))
{
// move tank down
}

// blah blah blah....

turret._x = tank._x;
turret._y = tank._y;



Then you will only have to worry which direction the barrel is facing. Which if you draw it correctly, can be determined by the ._rotation property. ;)

ChaosKindred
February 21st, 2005, 02:04 AM
You can also make a function that rotates MC's to the mouse or wuteva else u want them to:

function pointAt(ob, x, y,offset){
var dx = x - ob._x;
var dy = y = ob._y
var dist = Math.sqrt(dx*dx+dy*dy)
var angle;
if(dy<0){
angle = Math.PI*2-Math.acos(dx/dist);
} else {
angle = Math.acos(dx/dist);
}
ob._rotation = angle*180/Math.PI+offset;
}

put this in the first frame of your movie, then in the tank barrel add:

onClipEvent(enterFrame){
_root.pointAt(this, _root._xmouse, _root._ymouse, *wuteva the offset is*);
}

*offset depends on how you drew tank barrel, for example, if tank rotation is off by 90 degrees, add 90 in that field*

danulf
February 21st, 2005, 09:22 AM
yeah, thanks, but I already have the rotation for the barrel and the movement for the tank... It's just that the barrel and the tank are seperate MC's, so the tank moves, but the barrel stays :) I'm looking for a way to make the Barrel MC and the tank MC stuck to eachother... If you understand what I mean... Thanks for the help though

ChaosKindred
February 21st, 2005, 12:52 PM
add in on ur tank
if(key.isDown(Down)){
tank._y+= wuteva
turret._y+=wuteva
}

and do that for all ur movements. I had the same prob, the call to put the turret on the tank comes after the call to move it, which causes the turret to lag. If thats wut ur talking about

Marz
February 21st, 2005, 04:10 PM
I would have personally put the turret movieclip inside of the tank movieclip.

_root.tank.turret._rotation = blah blah;

That will only rotate the turret, but if you were to do this :

_root.tank._x += 5;

It would move the tank and the turret.

Copes
February 22nd, 2005, 11:19 AM
i use that code for moving my character marz, but when i say move and it palys animation_1 and press attack and it plays fight animation, it gets confused? is there a way to specify to do the attack one above all the others?

and when pressing multiple keys it has issues with my key releaseing code to stop the anmations playing. could you post the code for key ups that you would use?