Creating and destroying lots of the same sprite

For questions about using Classic.

Post » Sun Oct 04, 2009 7:10 pm

Hi,

I'm new to construct and only been using it a few weeks but I'm slowly making progress each week. I have another question which I have searched the forum all afternoon but couldn't find an obvious answer. So the question is...

Think of a side-scroller, space shoot'n'up and one common occurrence are asteroids flying from the right to the left side if the screen. I have set up random sprites to float across the screen using a combination of bullet behaviour with random speeds. However spinning asteroids present the problem of when you change the rotational angle of the sprites it drastically changes the bullet direction (spin round in circles kind of thing). To get round this I created dummy sprites that use bullet behaviour and now I'm trying to set the XY position of the actual asteroid graphic on top of the dummy bullet sprite using the always condition to track position, however the set position sort of works, I get multiple asteroid sprites on top of each other. The other problem is tracking all the asteroids sprites! There's only ever about 15 sprites on screen at any one time. I don't know how to destroy the asteroids sprites because Im not sure how to identify which sprite is which. If I destroy 'asteroid sprite' they all disappear.


Any ideas?
My Games on google play

ImageImage
B
30
S
6
G
4
Posts: 97
Reputation: 3,008

Post » Sun Oct 04, 2009 7:32 pm

This is a problem best solved using containers.

Link to wiki article on containers

If you add your asteroid sprite into a container with the dummy sprite, the asteroid will be paired with the dummy object. When it comes to destroying things, when one object in the container is destroyed -- say, the dummy object -- the asteroid object will be destroyed with it. And when it comes to creating objects, if you create a new dummy object, a new asteroid object will be created with it.

As you can see, containers are really nifty things. :)
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Sun Oct 04, 2009 8:16 pm

Don't use the bullet behaviour at all, Just set the sprites X position.

Loop a creation event how ever many times for the amount of asteroids you want. Create them outside the window size(so if its 640x480, set their position to 700 etc)

Then when they pass out of the Left Display edge return them to the other side.


SO, you have

CreateSprite:Asteroid at 700+random(200),random(480)
Run event 100 times

Always:Asteroid.X=6*60Timedelta
:Add 2 to rotation(center the hotspot/image point for central rotation, off center for more interesting rotation)

Asteroid.X -100 DisplayLeft:set position 700+random(200), random(480)
:set Asteroid width to:50+random(20)
:set Asteroid height to:50+random(20)


That should get it going, and you can do all sorts of things with it, add more variation in size, speed, positioning, rotation etc
B
9
S
2
G
4
Posts: 346
Reputation: 2,726

Post » Sun Oct 04, 2009 9:28 pm

Thank you both i will rework what i've done.

Apologies for the typos before, had written my question from a mobile phone!
My Games on google play

ImageImage
B
30
S
6
G
4
Posts: 97
Reputation: 3,008

Post » Sun Oct 04, 2009 10:19 pm

No problem. :) Just thought I'd mention that you could also use the ball behavior instead of the bullet behavior. The ball behavior allows you to have the movement angle and display angle separate by ticking the "Rotate object" box off in the behavior properties.
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 6 guests