@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:
- Where you used to ‘attach’ (start listening for) enterFrame, setLinearVelocity(0,-400) like you showed above.
- 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) print(string.format("Game Scene: X: %d, Y: %d", self.x, self.y)) buffer = buffer or 0 local minX = display.contentCenterX - display.actualContentWidth/2 + self.contentWidth/2 + buffer local maxX = display.contentCenterX + display.actualContentWidth/2 - self.contentWidth/2 - buffer local minY = display.contentCenterY - display.actualContentHeight/2 + self.contentHeight/2 + buffer local maxY = display.contentCenterY + display.actualContentHeight/2 - self.contentHeight/2 - buffer if( self.x \< minX ) then self.x = minX end if( self.x \> maxX ) then self.x = maxX end if( self.y \< minY ) then self.y = minY end if( self.y \> maxY ) then self.y = maxY end end
jumper.enterFrame = function(self, event) -- self:applyForce(0 \* self.mass, -200 \* self.mass, self.x, self.y) -- jumper:setLinearVelocity(0, -400) -- Jumper is jumping with -300 to jumper.y jumper:applyLinearImpulse(0, -200, jumper.x, jumper.y) jumperOnScreen(jumper) 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??
minX = 50 maxX = display.contentWidth minY = 50 maxY = display.contentHeight - 150 -- checks the x = 0 if self.x - self.width \* 0.5 \< minX then self.x = minX elseif self.x - self.width \* 0.5 \> maxX then self.x = maxX elseif self.y - self.height \* 0.5 \< minY then self.y = minY elseif self.y - self.height \* 0.5 \> maxY then self.y = maxY
Gives you 10 bucks or 10 minutes??
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. Thanks by the way.