How to change the image of my missiles without making a new class for each?

Subscribe to How to change the image of my missiles without making a new class for each? 9 posts, 4 voices

Sign in to reply


 
avatar for Penguin941 Penguin941 87 posts
Flag Post

The ship can shoot 12 different bullets, they all are the same, just look differently. I want to use a random number 1-12 and attach a different MC to my ship when he shoots for each number representing a different bullet.

How would I do this without making 12 classes, because whenever I change

var missile = _root.attachMovie( "Missile" , "Missile" + _root.getNextHighestDepth(), _root.getNextHighestDepth() );
				

and change the “Missile” part to another movie clip name, it doesn’t work.

Thanks in advanced.

 
avatar for Cervello Cervello 76 posts
Flag Post

This topic may help.
If you want to keep the above method, I’ll need to see what you actually did with the code to give you an opinion as to why it may not be working.

 
avatar for Penguin941 Penguin941 87 posts
Flag Post

How do I attach any movie clip with that function in any class?

That code is in my ship class. What are the parameters of the “Missile” is that the MC i have named missile or the class I have named missile?

 
avatar for Cervello Cervello 76 posts
Flag Post

Once you replace the 12 movieclips with one Missile MC with 12 frames, change the code above to:

var missile = _root.attachMovie( "Missile" , "Missile" + _root.getNextHighestDepth(), _root.getNextHighestDepth() );
missile.gotoAndStop(Math.floor(Math.random()*11)+1); //stops missile MC at random frame from 1 to 12

If that doesn’t work, try:

var mName:String = "Missile" + _root.getNextHighestDepth();
var missile = _root.attachMovie( "Missile" , mName, _root.getNextHighestDepth() );
_root[mName].gotoAndStop(Math.floor(Math.random()*11)+1); //stops missile MC at random frame from 1 to 12
 
avatar for Penguin941 Penguin941 87 posts
Flag Post

That’s a really good idea, I’m going to try your first method. Thankyou, I’ll post here if I get it to work or not later.

 
avatar for MoonlaughMaster MoonlaughMaster 3277 posts
Flag Post

Actually your first method would make it stop at one to 12, because if the random # gets floored to zero, the +1 keeps it from doing that. So it should be:

_root[mName].gotoAndStop(Math.floor(Math.random()*12)+1);
 
avatar for Cervello Cervello 76 posts
Flag Post

Actually your first method would make it stop at one to 12

Oh, right. 12 frames = frames 1~13. My bad.

 
avatar for Penguin941 Penguin941 87 posts
Flag Post

Thanks, it worked :)

 
avatar for Kalinium Kalinium 767 posts
Flag Post

Couple of things:

For choosing a random integer, you can use Math.random(n) to get an integer from 0 to [n-1]

Math.random(11)+1

The best method of choosing a graphic would probably be having each image on a different frame.

Slightly offtopic, but in the thread you linked to, Cervello, you were saying that it isn’t possible to colour things in via ActionScript, unless they were drawn in AS, as far as I understood it. This isn’t true – it’s possible to set the colour of MovieClips. In the example of changing the character’s colour, I would make the coloured parts of the character into a MovieClip (ie, shirt, hat, etc) and leave the other parts (skin, etc). Then:

colour = Math.round(0xFF0000)
colourObject = new Color(myShirtMC)
colourObject.setRGB(colour)

To set myShirtMC to red, for example

Sign in to reply


Click Here