Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Box2D Gamplay problem — Gideros Forum

Box2D Gamplay problem

GhostLeaderGhostLeader Member
edited March 2014 in General questions
Hi i have new problem in my gameplay character i figure out how to do a jump movement but i still have a problem with circle shape, every frame is saving the last position and creating a pink circle shape can someone help me plz ?

for the frame function:
-- Function to enter frame
function level:onEnterFrame()
	-- edit the step values if required. These are good defaults!
	self.world:step(1/60, 8, 3)
	--iterate through all child sprites
	for i = 1, self:getNumChildren() do
		--get specific sprite
		local sprite = self:getChildAt(i)
		-- check if sprite HAS a body (ie, physical object reference we added)
		if sprite.body then
			--update position to match box2d world object's position
			--get physical body reference
		local body = sprite.body
		-- update object's position to match box2d world object's position
		-- apply coordinates to sprite
		body.object:setPosition(body:getPosition())
		--apply rotation to sprite
		body.object:setRotation(math.deg(body:getAngle()))
		end
	end
end
for my main character :
mainBosso = Core.class(Sprite)
 
function mainBosso:init(level, x, y)
	self.level = level
	self:setPosition(x,y)
 
	--create bitmap object from ball graphic
	self.bitmap = Bitmap.new(self.level.g:getTextureRegion("BossoBleu.png"))
	--reference center of the ball for positioning
	self.bitmap:setAnchorPoint(0.5,0.5)
	self:addChild(self.bitmap)
 
	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
	self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
end
 
function mainBosso:createBody()
	--get radius
	local radius = (self.bitmap:getWidth()/2)*0.85
 
	--create box2d physical object
	local body = self.level.world:createBody{type = b2.DYNAMIC_BODY}
 
	--copy all state of object
	body:setPosition(self:getPosition())
	body:setAngle(math.rad(self:getRotation()))
	local circle = b2.CircleShape.new(0, 0, radius)
 
	local fixture = body:createFixture{shape = circle, density = 1, 
	friction = 0.1, restitution = 0.8}
 
	body.type = "main"
	self.body = body
	body.object = self
	table.insert(self.level.bodies, body)
end
 
-- Jump Function 
function mainBosso:onMouseDown()
	self.startX = self:getX()
	self.startY = self:getY()
end
 
function mainBosso:onMouseUp()
		self:createBody()
		--applye impulse to target
		self.body:applyLinearImpulse(0, -10, self:getX(), self:getY())
		self:removeEventListener(Event.MOUSE_UP, self.onMouseDown, self)
end

Comments

  • GhostLeaderGhostLeader Member
    edited March 2014
    and i know how to scroll a parallax ground but if there's a tutorial with how to scroll the ground with generated object it will be nice :)
  • ar2rsawseenar2rsawseen Maintainer
    @GhostLeader that could be because you call self:createBody() on each mouse up event
    meaning every time you release a mouse/finger, it creates a new body

    You should create body once inside init and use it

    Also about paralaxing, then there are two ways:

    1) actually moving box2d object through the world (what most of platformers do)
    and could be done something like this:
    http://appcodingeasy.com/Gideros-Mobile/Gideros-Camera-Move

    2) keeping the body in the same place and moving the world:
    http://giderosmobile.com/forum/discussion/comment/27868#Comment_27868
  • thank you again and how can you hide ChainShape or do i just place them behind my background
  • ar2rsawseenar2rsawseen Maintainer
    What do you mean hide? If ChainShape is showing, it means debug draw is on, just search and comment out debugDraw :)
  • no it's fine i hide it :)
  • actually disabling debug draw is more good by doing so you can save some extra drawings of physics body putting them behind background will still draw them and put some extra load on performance

    :)
  • ar2rsawseenar2rsawseen Maintainer
    @hgvyas123 so true
    and actually not simple extra load, but a lot of load, debug draw is very very unefficient
  • actually im having a problem on positioning my background , the ground and my pause button and restart button because of the new camera movement can someone help me ?
    and i have a problem with createLayer() function if #self .layers > 2 i got the problem classes/GiderosCodingEasy.lua:125: index '__userdata' cannot be found
    stack traceback:
    classes/GiderosCodingEasy.lua:125: in function 'removeChild'
    Scenes/level.lua:280: in function 'createLayer'
    Scenes/level.lua:306: in function

    here's my level.lua
    lua
    lua
    level.lua
    9K
  • ar2rsawseenar2rsawseen Maintainer
    @GhostLeader to deal with running away pause and restart buttons, etc there are 2 options
    1) add them to the stage and remove when scene exits
    2) create a separate layer for camera and add all worlds object to that, thus the scene itself does not move

    About the second, basically error happens here:
    self.layer:removeChild(self.layers[1].layer)

    meaning that either the bitmap was not saved or was already removed
    you can try changing it to something like this:
    if self.layers[1].layer then
        self.layers[1].layer:removeFromParent()
        self.layers[1].layer = nil
    end
  • and how can i use setLinearVelocity with other class object because i have a class for my main character and i want to use createBody() not only self.level.world:createCircle(self.bitmap, {type = "dynamic"})
  • ar2rsawseenar2rsawseen Maintainer
    Sorry I don't really get the last one.
    Usually what I do is save reference to a body, like
    local bmp = Bitmap.new(...)
    --then add physical body reference
    bmp.body = body
     
    -- and when you need to push bmp somewhere, you simply use
    bmp.body:setLinearVelocity --etc
Sign In or Register to comment.