How can I change a picture to another picture on collision? In my game, there is an object that is falling, and I want the object to change to another picture when it hits another object. I already have the collision set up, I just want to know what to type where the collision starts. Here is what I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | --START PHYSICS local physics = require("physics") physics.start() --CREATE OBJECTS local ball = display.newImage("ball.png") physics.addBody( ball, { density=0.9, friction=0.3, bounce=0.3} ) local object = display.newImage("object.png") --COLLISION local function onLocalCollision( self, event ) if ( event.phase == "began" ) then --CHANGE BALL TO "otherball.png" end end ball.collision = onLocalCollision ball:addEventListener( "collision", ball ) object.collision = onLocalCollision object:addEventListener( "collision", object ) |
You can't change image once you have created image object, your only solution is to destroy current image object and create new object.
On second thought, you can use sprite sheets to achieve what you want. You can change frames of sprite object on collision.
Ok so how do I do that? Can you just write me a couple lines of code or something to show me? Thanks!
You need to learn how to use spritesheets. It's not easy to explain concept in few lines of code. Try going through sprites sample app.
Have a look at movieClips first though, as I'm sure this can be done with them and the pain and suffering is greatly reduced :)
-- Chris