object:applyForce problem

@Roaminggamer, I mean where to put that function? By just calling it and can use or need to put inside some enterFrame listener to use it? 

If you use setLinearVelocity() be sure to later set the velocity to zero or it will keep moving forever.

i.e.  instead of using a enterFrame() with a force, do the following:

  1. Where you used to ‘attach’ (start listening for) enterFrame, setLinearVelocity(0,-400) like you showed above.
  2. Where you used to ‘detach’ (stop listening for) enterFrame, setLinearVelocity(0,0)

I would add a enterFrame listener to the player (xxx) and have it run forever.  Then, in the enterFrame listener put a derivative of the code I wrote.

It is very brute force, but will keep the object on screen.

Note: For ‘jumping’, applyLinearImpulse() is more suitable than velocity or force.

I write like

local function jumperOnScreen(self, event) &nbsp; print(string.format("Game Scene: X: %d, Y: %d", self.x, self.y)) &nbsp; &nbsp;buffer = buffer or 0 &nbsp; &nbsp;local minX = display.contentCenterX - display.actualContentWidth/2 + self.contentWidth/2 + buffer &nbsp; &nbsp;local maxX = display.contentCenterX + display.actualContentWidth/2 - self.contentWidth/2 - buffer &nbsp; &nbsp;local minY = display.contentCenterY - display.actualContentHeight/2 + self.contentHeight/2 + buffer &nbsp; &nbsp;local maxY = display.contentCenterY + display.actualContentHeight/2 - self.contentHeight/2 - buffer &nbsp; &nbsp;if( self.x \< minX ) then self.x = minX end &nbsp; &nbsp;if( self.x \> maxX ) then self.x = maxX &nbsp;end &nbsp; &nbsp;if( self.y \< minY ) then self.y = minY end &nbsp; &nbsp;if( self.y \> maxY ) then self.y = maxY end end

jumper.enterFrame =&nbsp; &nbsp; &nbsp; &nbsp; function(self, event) &nbsp; &nbsp; &nbsp; &nbsp; -- self:applyForce(0 \* self.mass, -200 \* self.mass, self.x, self.y) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- jumper:setLinearVelocity(0, -400) -- Jumper is jumping with -300 to jumper.y &nbsp; &nbsp; &nbsp; &nbsp; jumper:applyLinearImpulse(0, -200, jumper.x, jumper.y) &nbsp; &nbsp; &nbsp; &nbsp; jumperOnScreen(jumper) &nbsp; &nbsp; &nbsp; end

For every time, it doesn’t go beyond the minY, but for MaxY, it goes beyond the screen. So when I jump, it is not going beyond the roof but it can goes until the bottom and cannot see it anymore

No.  Don’t put an impulse in an enterFrame call.  That isn’t what I suggested.

Give me 10 and I’ll post an example.

** UPDATE ** 10 minutes

This is how I wrote for the function. It is still not working for my project though I think I need to call this function inside some functions or?? :smiley:

 minX = 50 &nbsp; maxX = display.contentWidth &nbsp; minY = 50 &nbsp; maxY = display.contentHeight - 150 &nbsp;&nbsp; &nbsp; -- checks the x = 0 &nbsp; if self.x - self.width \* 0.5 \< minX then &nbsp; &nbsp; self.x = minX &nbsp; &nbsp;&nbsp; &nbsp; elseif self.x - self.width \* 0.5 \> maxX then &nbsp; &nbsp; self.x = maxX &nbsp; &nbsp;&nbsp; &nbsp; elseif self.y - self.height \* 0.5 \< minY then self.y = minY &nbsp; &nbsp;&nbsp; &nbsp; elseif self.y - self.height \* 0.5 \> maxY then &nbsp; &nbsp; self.y = maxY

Gives you 10 bucks or 10 minutes?? :smiley:

Here look at this example:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/05/jump.zip

-- ============================================================= -- Your Copyright Statement Goes Here -- ============================================================= -- main.lua -- ============================================================= -- Empty Framework - Generated using 'EAT - Frameworks' -- https://gumroad.com/l/EATFrameOneTime -- ============================================================= io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) local function stayOnScreen( obj ) buffer = buffer or 0 local minX = display.contentCenterX - display.actualContentWidth/2 + obj.contentWidth/2 + buffer local maxX = display.contentCenterX + display.actualContentWidth/2 - obj.contentWidth/2 - buffer local minY = display.contentCenterY - display.actualContentHeight/2 + obj.contentHeight/2 + buffer local maxY = display.contentCenterY + display.actualContentHeight/2 - obj.contentHeight/2 - buffer local correctedX = false local correctedY = false if( obj.x \< minX ) then obj.x = minX; correctedX = true end if( obj.x \> maxX ) then obj.x = maxX; correctedX = true end if( obj.y \< minY ) then obj.y = minY; correctedY = true end if( obj.y \> maxY ) then obj.y = maxY; correctedY = true end local vx,vy = obj:getLinearVelocity() if( correctedX ) then vx = 0 end if( correctedY ) then vy = 0 end if( correctedX or correctedY ) then obj:setLinearVelocity( vx, vy ) end end local physics = require "physics" physics.start() local boy = display.newImageRect( "images/boy.png", 80, 90 ) boy.x = display.contentCenterX boy.y = display.contentCenterY + 300 physics.addBody( boy, "dynamic" ) function boy.enterFrame( self ) stayOnScreen( self ) end Runtime:addEventListener( "enterFrame", boy ) function boy.touch( self, event ) if( event.phase == "ended") then self:applyLinearImpulse( 0, -10 \* self.mass, self.x, self.y ) end return true end Runtime:addEventListener( "touch", boy )

Note: This will be my last answer for the day.  I’ve gotta get back to work.

Ok, thanks, me too. I need to sleep so this post is my last post. :slight_smile: Thanks by the way. :slight_smile: