Hey guys,
I'm from the procedural programming world and new to Gideros and still trying to wrap my head around how the objects interact with each other and getting event handlers to trigger correctly (and many many more...).
Here's a quick one for you... I want my explosion movieclip to run where and when the grenade makes contact with the wall boundaries and be removed once the movieclip has finished running. I've included my code and as you will see, it pretty much is a disaster ;-)
I will greatly appreciate it if you can have a look and fix it up for me - after having tried everything I can think of, I think that the penny might just drop, once I see what the code is supposed to look like.
I originally thought that the best way to go about this was to call the whole definition and execution of the movieclip in a function on a contact event - is it?
Please excuse all the commented out code ... that's from all my trail and error efforts.
txt
txt
Comments
Yes it always hard to switch between paradigms
I could not run your project because I have no other image etc assets, but it seems to be quite ok, if you are having only single scene. But if your game will have more scenes, you should add the elements to the scene and not the stage.
About your problem, it looks you already had all the code, like inside onBeginContact you need to do:
body:setPosition(explode:getX(), explode:getY())
there is nothing really you need to do anymore.
So what is not working exactly?
Thanks for your reply!
There are more scenes to come yes, so I'm guessing I must make use of the Scene Manager, which I've seen being talked about a couple of times, right?
When I run the code, it gives me the "attempt to call method 'getX' (a nil value)" error inside onBeginContact when I try to get the coordinates for bodyA (or bodyB). This seems to me that the bodies don't hold value, but this can't be the case, else onBeginContact would not have triggered?
Also, when I call the explosion function from within onBeginContact, I get an error that the World is locked where I create the body for the explosion. From what I understand the World is locked on events like BeginContact, so I should probably define the movieclip and just set the position and run the clip on a contact event. I still need the coordinates for my explosion to run though.
Thanks a lot for your help! If you want to, I can send you the assets so that you can run the code?
so here is a hack for onBeginContact method:
https://github.com/gideros/Scene-Manager
And you can also check out the Game Template from here:
https://github.com/ar2rsawseen/GameTemplate
Now my explosion starts to run where the contact is made, as I wanted it to. It blows my ball out of the park though and creates new explosions all the way, but I guess I must run the explosion in it's own layer in order to get it to overlay .... but that's the next step. I'm happy to have got this bit to work. Thanks again for your help, as well as for all your tutorials, I refer to them just about every time I work with Gideros!
Likes: ar2rsawseen
Yep, different layers makes the most sense to me too - I'm still to add scenes to my project and code for different layers (and how to do/work with it)
For now, I achieved the same result by destroying the fixture right at the end of my function that creates and runs the explosion. I don't think it's a good way of doing it though, but at least it helped me to understand a bit more about how all the elements of an object fit together.... trail and error can be fun sometimes! :-)