So I create an enemy sprite, and with the same enemy some are supposed to go left to right and others are supposed to go right to left. Think of the cars in the Frogger video game [some going left to right and others going right to left].
Seems simple enough but no matter what action or behavior I use, no matter what event I put the code in … it doesn’t work correctly. when new enemies are created the direction of every enemy is changed.
#1 - I tried using permanent force in an event…using an object variable [if “mydirection” = 0, then to use for going right]. that didn’t work
#2 - I tried adding the top-down behavior…but it appears even when I “select” the right enemies with the object variable…ALL enemies have their direction changed [even the enemies that are not “supposed to” be selected].
WHAT AM I DOING WRONG? Below are screen shots of 2 completely different attempts in different projects…neither worked].
Can you PLEASE help? Thank you in advance for your help on this simple [but confusing] matter.
How are you spawning the objects? How are the variables MyDirection and JustSpawned being set?
This is one way to do it using the spawn behavior on the SpawnPoint object. IDK how you’re deciding the direction. I used a random number. It will either be 0 or 1. So, 0x180 or 1x180.
Sorry wrong pic
I saw the “spawn” command for the first time in your code [I was not aware of that command before then]. I am creating new objects in a sub-event, but with a create object command…shown below:
Can you please let me know what [or where] my mistake is?
Ok. I like to create them myself sometimes too. When you’re setting the JustSpawned to true I believe you’re changing all of the instances to true both new and old.
If you set the object variable JustSpawned default value to true thru the main object setting thingy. (I can’t think of it’s real name) Then the objects will be created with JustSpawned set to true then you just need to check if true then set to false and apply the force.
Edit: it looks like the direction is always being set to 0. If you set the direction to a random number first then you could use the direction variable in the set animation action.
I totally believe you are correct, and I will change the variable in the main editor [I know exactly what you’re referring to]. However, wouldn’t that problem still exist with the “MyDirection” object variable?
In another words, when I create a new object and randomly set its “MyDirection” to 0 or 180 wouldn’t that also screw things up and change the value for all enemies [both old and newly created]?
How can you modify a single instance of an object [its variables, etc.] without doing a global change for all instances on the screen?
This is mind boggling. In the meantime, I will implement your suggestion of changing the default value via the editor.
Yes. I would use a subevent to your add object similar to my example where I used false, you could use true.
JustSpawned of object is true…
(That will filter out the other objects)
All of your actions to set direction, movement etc. You could add another subevent of that to flipped the object if the direction is left.
Instances can be frustrating.
yes instances are very frustrating. thanks a million for all of your help and the time you’ve committed to this topic. Let me play around with this craziness and if or when I find a solution that works in my particular case I will post it here.
It’s a simple thing, yet I think other’s may have run into it and can benefit from any solutions.
You’re welcome. I’m not sure if you’re using a timer or the spawn behavior. So, here’s both ways.
I’m on my PC now, it’s a lot easier than on a phone
My logic before was flawed. Sorry. For the timer you don’t need to filter the objects but for the spawner you do. I got the 2 concepts mixed. I’ll change it above but I wanted to post it here too.