How do I create random enemy sprites using instance variables?

0 favourites
  • 13 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi,

    I'm making a very simple shooter game and want the game to randomly spawn three different types of enemy.

    The enemy's attributes are all identical; the only difference is how they look. I've read you can do this

    by using instance variables but can't work out how to do it at all. Any advice would be greatly appreciated.

  • You don't need instance variables for that. Just have all three enemies as members of a family and select the family when creating the enemy. It will pick a random enemy from that family.

    If you can't or won't use families, you can just use Create Object (by name). Just name all enemies "enemy0", "enemy1", etc. And create "enemy" & floor(random(total_enemy_types))

    In this example I use both methods. Just set the Boolean "useFamilies" to true, for using families, or false for using create object (by name).

    Hope it helps...

    Cheers!

  • Hi brunopalermo,

    Thanks so much - I used the non-family method as I'm currently using the free version of Construct 3, and it worked. :) Unfortunately I've run into a couple of other issues.

    Is there a way to stop enemies randomly spawning right next to/on top of the player sprite?

    Is there a way to change the event sheet actions for all the enemy objects in one go if I'm not using the Family method, or do I have to add each enemy object separately?

    Thanks again for your help.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Is there a way to stop enemies randomly spawning right next to/on top of the player sprite?

    The easy way is to keep spawn points outside of the viewport. You would spawn then very close to the viewport but out if sight. This might not fit the concept of the game you're making. Another option is to add the player position to the calculation of spawn position. Or you can just reposition those that overlap, as I did in the example below.

    Is there a way to change the event sheet actions for all the enemy objects in one go if I'm not using the Family method, or do I have to add each enemy object separately?

    You may have all enemies in one sprite only. In that case you'd have to modify a bit the code I sent you.

    I updated the file I've sent before, so just download it and check the code.

    Move the player with the arrows. I greatly reduced the enemy spawn interval so you can see better how they never spawn over the player. Now, enemy types are different animations of ONE enemy only. Instead of picking an enemy, you just set their animations. Since they're one sprite, all behaviors are the same.

    Hope this helps...

    Cheers!

  • Thanks again, brunopalermo, this method has saved me a lot of time - until now I was adding every enemy action in separately.

    I've got the enemy sprites to spawn correctly now, except when it comes to the "destroy" animation. I can only get one out of the three destroy animations to appear - how do I get the other two enemy sprites to display the correct animation frames?

  • I'm not sure I follow, but...

    In this method, since each enemy type is an animation, you should name your animations accordingly.

    For instance...

    You have 3 enemies, each with 3 animations (moving (0), shooting(1) and dying(2)). You should have a "type" instance variable set when creating an enemy.

    The Enemy sprite would have the following animations:

    • e0a0 - Moving animation for enemy type 0.
    • e0a1 - Shooting animation for enemy type 0.
    • e0a2 - Dying animation for enemy type 0.
    • e1a0 - Moving animation for enemy type 1.
    • e1a1 - Shooting animation for enemy type 1.
    • e1a2 - Dying animation for enemy type 1.
    • e2a0 - Moving animation for enemy type 2.
    • e2a1 - Shooting animation for enemy type 2.
    • e2a2 - Dying animation for enemy type 2.

    So, when an enemy dies you would "Set Animation" to "e" & Self.type & "a" & 2

    When an enemy is moving you would "Set Animation" to "e" & Self.type & "a" & 0

    And when an enemy is shooting you would "Set Animation" to "e" & Self.type & "a" & 1

    Got it? If it's not clkear, let me know and I'll add this to that example

    EDIT: I just updated that example so now every enemy has a moving animation and a dying animation.

    Move player with A, S, D and W. Aim with MOUSE and shoot with LEFT MOUSE BUTTON.

    Hope this helps...

    Cheers!

  • Thanks again brunopalermo, that was exactly what I wanted. :)

    Just to small questions: is there a way to make the enemies stop moving towards the player sprite once they start the "dying" animation?

    All my enemy sprites face the right, but sometimes they spawn upside-down and I can't work out how to stop this happening.

  • Done. Just download the file again.

    Regarding the direction. If you're using bullet behavior, make sure the "set angle" setting is active.

  • Many thanks brunopalermo, disabling the bullet behaviour worked.

    I've tried resetting the "set angle" setting as I was using bullet behaviour, but it only seems to fix enemies spawning on the right and middle of the screen. Anything coming from the left is still displaying upside down.

  • May you share a file? It's probably too specific, so it wuld help if I could take a look at the actual code.

  • Hi, this is going to sound like a dumb question, but what type of file do I upload, and how do I do this?

  • The easiest way is saving on the cloud (Dropbox, GDrive, etc) and granting access. You can do that from the save dialog in Construct 3. Or you can save the .c3p file locally and upload it to wherever you like.

    Cheers!

  • Hi, thanks for the explanation. Link to the file is here: drive.google.com/file/d/1kyr1NrxY7vLx4V7y2NyI85U4561uG7Qw/view

    For some reason the "pushDistance" global number now seems to be preventing the enemies from killing the player. It wasn't like this earlier so I must have messed something up somewhere, but for now I just want to work out why sprites spawning on the right hand side of the screen are flipped.

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