Please use code formatting. Click the blue <> button in the row with Bold, Italic, etc. Paste your code into the popup window. It will make your code easier for community members to read and understand.
Thanks
Rob
Please use code formatting. Click the blue <> button in the row with Bold, Italic, etc. Paste your code into the popup window. It will make your code easier for community members to read and understand.
Thanks
Rob
local physics=require("physics") physics.start(); physics.setGravity(0,0); local wall=display.newImageRect("c.png",200,100); wall.x=200; wall.y=250; physics.addBody(wall,"dynamic",{density=.1,friction=.2}); wall.myName="wall"; local function active(self,event) self:applyForce(0,-1.5,self.x,self.y); end local function touchscreen( event ) local phase=event.phase; if ("began"==phase)then wall.enterFrame = active Runtime:addEventListener("enterFrame",wall) end if( phase == "ended" ) then wall:removeEventListener("enterFrame",wall) end return true; end Runtime:addEventListener("touch",touchscreen);
my problem is, whenever my touch event is ended…my object doesnt stop…
Thanks for putting the code in a code block but remember you also have to take the time to make it legible. If that means posting, re-editing, re-posting. Please do it.
You’re asking us to spend our time to solve a problem you are stuck on, so you have to make it as easy as possible for us to help you.
This is how I would correct the issue you are encountering, or at least as I understand it.
You want the ‘wall’ to move when touching the screen and to stop when you are not touching the screen. I am also guessing you don’t want it to rotate.
(Notice the clean and tight formatting of this post)
local physics=require(“physics”) physics.start() physics.setGravity( 0, 0 ) local wall = display.newImageRect( “c.png”, 200, 100 ) wall.x = 200 wall.y = 250 wall.myName = “wall” wall.isMoving = false physics.addBody( wall, “dynamic”, { density = 0.1, friction = 0.2 } ) wall.isFixedRotation = true – This is a table listener – I find them superior because (among other reasons) they are easy to clean later function wall.enterFrame( self ) if( self.isMoving == false ) then return end self:applyForce( 0, -1.5, self.x, self.y ) end – This is another table listener function wall.touch( self, event ) local phase = event.phase if( “began” == phase ) then self.isMoving = true elseif( phase == “ended” ) then self.isMoving = false self:setLinearVelocity( 0, 0 ) end return true end – This function will clean up (remove) the – Runtime table-listeners when you delete the wall function wall.finalize( self ) Runtime:removeEventListener( “enterScreen”, self ) Runtime:removeventListener( “touch”, self ) end – Start listening wall:addEventListener( “finalize” ) Runtime:addEventListener( “enterFrame”, wall ) Runtime:addEventListener( “touch”, wall )
You have expressed confusion about Runtime:* listeners.
This should help you start to understand listeners in general:
https://docs.coronalabs.com/daily/guide/events/detectEvents/index.html
If you’re still reading this and feel the answer I gave helped you out, can you give me five minutes or less?
Please see this post and submit an idea or two:
https://forums.coronalabs.com/topic/66772-ssk2-lets-make-1-brainstorming/
well i have changed my code and rewrote it,but this time my wall doesnt move at all…it just look like my touch event is not working and yeah i would love to submit some ideas .
I typoed… replace word ‘enterScreen’ with ‘enterFrame’.
Note: This was was all only tested in my mind…
Here is a working example: http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/12/moving_wall.zip
https://www.youtube.com/watch?v=K6qofD1f570&feature=youtu.be
thanks a lot