views:

37

answers:

1

So I am porting a game I started in html canvas to flash as3.

In this game there is a tank. This tank has a body and a turret. It can rotate 360 degrees and move foward and backwards and swivel it's turret 360 degrees. The tank base and the turret are seperate animated images.

I have set it up so that there is a tank movieclip and a turret movieclip. And I have added code to the tank movieclip so that it creates a turret for itself in the constructor part like this.

 turret= new turretMovieClip();   
 addChild(turret)

So the tank movieclip class now has this turret variable that holds an instance of the turret movieclip. Also in the constructor of the tank movieclip, there is code to give itself a shadow.

var dropShadow:DropShadowFilter = new DropShadowFilter();
dropShadow.distance = 5;
dropShadow.angle = 45;
dropShadow.color = 0x000000;
etc....
this.filters = new Array(dropShadow);

But this is the weird part. I expected the code above to only put a shadow around the tank because this is code for the tank movieclip. But it put a shadow around the tank and the turret. When I added the turret, the tank movieclip changed or something! I did not want this to happen. I want the tank to have it's own shadow and the turret to have it's own shadow.

Here is an image I made illustrating the problem: http://i.imgur.com/EJuf3.jpg

Fig #1 is from my old game. The shadow correctly casts itself over the tank and the ground. Fig #2 shows what the above code did. It gave a shadow for the tank and turret, but it only casts over the ground. Anyone know what I am doing wrong? I think I could fix this by making a master tank class that then creates a tank movieclip and then a turret movieclip. Is that how I'm supposed to do this?

+2  A: 

The simplest would be to start off with an empty Sprite for the Tank class composed by two separate child nodes for the base / turret. That way you can apply two different shadows for these.

   + Tank [Sprite]
   |
   \--+ Base [MovieClip] = Graphics + DropShadow
   |
   \--+ Turret [MovieClip] = Graphics + DropShadow
Theo.T
Yep, this will do it. If the base isn't separated, the DropShadowFilter only applies to the outside edge of the entire composite object, adding a shadow to each individual object will get you what you want.
Tegeril
Thank you. It works fine now. I'm a bit unsure of which object should hold which variables and do the computing (right now I've put all of it in tank, but I guess it makes more sense for the base to hold the x,y, it's own rotation, speed, etc, and for the turret to hold its own rotation, rotspeed, etc) and whether or not I should update the tanks position to where the base and turret are all the time (I think it's unnecessary because it's invisible anyway right).Anyhow, here is a demo of it correctly working (WASD to move. Left and Right Arrow key to turn turret). http://db.tt/ihdAzi8 .
Tiby312
Keep it simple, I would put all the logic in the Tank and just control the position of the graphical objects from there. If some of the code relates specifically to one of the children (eg. a fire animation from the turret) just delegate it down to the object which is concerned.
Theo.T