Creating and destroying lots of the same sprite

This forum is currently in read-only mode.
From the Asset Store
Awardkit
$5 USD
Create custom awards and achievement or use the many templates and create your own.
  • 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 I�m not sure how to identify which sprite is which. If I destroy 'asteroid sprite' they all disappear.

    Any ideas?

  • 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.

  • 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

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

    Apologies for the typos before, had written my question from a mobile phone!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)