Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How to set position and play a movieclip in onBeginContact? — Gideros Forum

How to set position and play a movieclip in onBeginContact?

riaanoriaano Member
edited July 2013 in Game & application design
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.

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Hello @riaano ;)
    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:
    local x,y = bodyA:getX(), bodyA:getY()
    explosion(x,y)
    and inside explosion you need to uncomment
    explode:setPosition(x,y)
    since you are already assigning explosion coordinates to body
    body:setPosition(explode:getX(), explode:getY())

    there is nothing really you need to do anymore.

    So what is not working exactly? :)
  • riaanoriaano Member
    Hey ar2rsawseen,

    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?
  • ar2rsawseenar2rsawseen Maintainer
    edited July 2013 Accepted Answer
    Aha I see now.
    so here is a hack for onBeginContact method:
    --yes body does not have separate methods, but it has getPosition method 
    --which returns both x and y
    local x,y = bodyA:getPosition()
    --and to launch explode in another enter frame where world will be unlocked, 
    --here is a quick hack
    Timer.delayedCall(1, function()
        explode:setPosition(x,y)
    end)
    And about scenes, yes you should use sceneManager from here:
    https://github.com/gideros/Scene-Manager

    And you can also check out the Game Template from here:
    https://github.com/ar2rsawseen/GameTemplate
  • riaanoriaano Member
    Thanks a million! :-)
    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

    +1 -1 (+1 / -0 )Share on Facebook
  • plamenplamen Member
    @riaano running separate layer for explosions and other vfx is the best soluton just keep eye on loading sequence of the layers and you will be ok. I use layers in my project for each sea, clouds, actors , weapons( you don't wanna hulls to cover missiles ), vfx (explosions , poison gas blasts, smoke, electric charges etc) and i plan to put another cloud on top after i finish the level.
  • riaanoriaano Member
    Hey plamen,

    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! :-)

Sign In or Register to comment.