touch event

local physics=require(“physics”);

physics.start();

physics.setGravity(0,0);

local ball=display.newImageRect(“c.png”,200,100);

ball.x=300;

ball.y=200;

physics.addBody(ball);

local function activateball(self,event)

 self:applyForce(0,-1.5,self.x,self.y)

  end

local function tc(event)

   if event.phase=="began"then 

     ball.enterFrame=activateball

    Runtime:addEventListener(“enterFrame”,ball)

   end

    if event.phase=="ended"then

    Runtime:removeEventListener(“enterFrame”,ball);

  end

end

Runtime:addEventListener(“touch”,tc);

hii…i   wrote this program to learn touch event .Everything is good but there is a problem in last phase of touch(ended phase).when i just tap out ,my object  BALL doesnt stop at that point where i just tapped out .

and i also want to share that  i have learnt this method from tutorial        https://www.youtube.com/watch?v=_7kCULfpDWY      

Formatting your code to help future readers:

local physics=require("physics"); physics.start(); physics.setGravity(0,0); local ball=display.newImageRect("c.png",200,100); ball.x=300; ball.y=200; physics.addBody(ball); local function activateball(self,event) self:applyForce(0,-1.5,self.x,self.y) end local function tc(event) if event.phase=="began"then ball.enterFrame=activateball Runtime:addEventListener("enterFrame",ball) end if event.phase=="ended"then Runtime:removeEventListener("enterFrame",ball); end end Runtime:addEventListener("touch",tc);

One thing I can see right away is you need to set and release focus to make this work consistently.

https://docs.coronalabs.com/daily/api/type/StageObject/setFocus.html

This is better…

local function tc(event) local phase = event.phase local id = event.id local target = event.target if( phase == "began" ) then display.currentStage:setFocus( target, id ) target.isFocus = true target.enterFrame = activateball Runtime:addEventListener("enterFrame",target) elseif( target.isFocus ) then if( phase == "ended" ) then display.currentStage:setFocus( target, nil ) target.isFocus = false Runtime:removeEventListener("enterFrame",target) end end end

i have tried the way you suggested  but the problem remain same.whenever my touch event ended .my object doesnt  stop moving.

here is my code

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);

wall.myName=“wall”;

local function active(self,event)

  self:applyForce(0,-1.5,self.x,self.y);

end

local function  touchscreen( event )

  local wall=event.target;

  local id = event.id

  local phase=event.phase;

  if (“began”==phase)then 

    --set touch focus on the wall

    display.currentStage:setFocus(wall,id);

  wall.isFocus = true

      wall.enterFrame = active

      Runtime:addEventListener(“enterFrame”,wall)

  

 elseif( wall.isFocus ) then

      if( phase == “ended” ) then 

         display.currentStage:setFocus( wall, nil )

         wall.isFocus = false

         Runtime:removeEventListener(“enterFrame”,wall)

     end

   end

  return true;

end

wall:addEventListener(“touch”,touchscreen);

If I should make a guess… 2 things…

Your gravity is 0    -so no counter force to make the ball stop.

or

I don’t see anything telling the ball to stop in the code as a whole… 

In the top you send the ball moving: self:applyForce(0,-1.5, selfx,selfy)

Down at the:  if(phase == “ended”)  I think you are missing something to set the force to  zero again.

ah…let me try but if i had to make a resistance force than what is the purpose of command

 Runtime:removeEventListener(“enterFrame”,wall) 

i have tried everything but i dont know what to do to stop my object and how the command

 Runtime:removeEventListener(“enterFrame”,wall)      is working

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…

  1. 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.
     
     
     

  2. 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

@kumarchetan312,

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

Formatting your code to help future readers:

local physics=require("physics"); physics.start(); physics.setGravity(0,0); local ball=display.newImageRect("c.png",200,100); ball.x=300; ball.y=200; physics.addBody(ball); local function activateball(self,event) self:applyForce(0,-1.5,self.x,self.y) end local function tc(event) if event.phase=="began"then ball.enterFrame=activateball Runtime:addEventListener("enterFrame",ball) end if event.phase=="ended"then Runtime:removeEventListener("enterFrame",ball); end end Runtime:addEventListener("touch",tc);

One thing I can see right away is you need to set and release focus to make this work consistently.

https://docs.coronalabs.com/daily/api/type/StageObject/setFocus.html

This is better…

local function tc(event) local phase = event.phase local id = event.id local target = event.target if( phase == "began" ) then display.currentStage:setFocus( target, id ) target.isFocus = true target.enterFrame = activateball Runtime:addEventListener("enterFrame",target) elseif( target.isFocus ) then if( phase == "ended" ) then display.currentStage:setFocus( target, nil ) target.isFocus = false Runtime:removeEventListener("enterFrame",target) end end end

i have tried the way you suggested  but the problem remain same.whenever my touch event ended .my object doesnt  stop moving.

here is my code

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);

wall.myName=“wall”;

local function active(self,event)

  self:applyForce(0,-1.5,self.x,self.y);

end

local function  touchscreen( event )

  local wall=event.target;

  local id = event.id

  local phase=event.phase;

  if (“began”==phase)then 

    --set touch focus on the wall

    display.currentStage:setFocus(wall,id);

  wall.isFocus = true

      wall.enterFrame = active

      Runtime:addEventListener(“enterFrame”,wall)

  

 elseif( wall.isFocus ) then

      if( phase == “ended” ) then 

         display.currentStage:setFocus( wall, nil )

         wall.isFocus = false

         Runtime:removeEventListener(“enterFrame”,wall)

     end

   end

  return true;

end

wall:addEventListener(“touch”,touchscreen);

If I should make a guess… 2 things…

Your gravity is 0    -so no counter force to make the ball stop.

or

I don’t see anything telling the ball to stop in the code as a whole… 

In the top you send the ball moving: self:applyForce(0,-1.5, selfx,selfy)

Down at the:  if(phase == “ended”)  I think you are missing something to set the force to  zero again.

ah…let me try but if i had to make a resistance force than what is the purpose of command

 Runtime:removeEventListener(“enterFrame”,wall) 

i have tried everything but i dont know what to do to stop my object and how the command

 Runtime:removeEventListener(“enterFrame”,wall)      is working