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
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
and actually not simple extra load, but a lot of load, debug draw is very very unefficient
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
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:
Usually what I do is save reference to a body, like