It looks like you're new here. If you want to get involved, click one of these buttons!
-- MAIN.LUA part -- Create multiple blocks. for i = 1, 6 do -- The block image. local blockImage = Bitmap.new(Texture.new("Graphics/3D_Block3.png")) blockImage:setAnchorPoint(0.5, 0.5) -- The block value (later also + - : x) local blockValue = math.random(1000) local posX = (i * 150) + 32 local posY = 540 blockBody = physicsWorld:createBody{type = b2.DYNAMIC_BODY, position = {x = posX, y = posY}} local shape = b2.EdgeShape.new(0, 0, 144, 80) blockBody:createFixture{shape = shape, density = 0.1, restitution = 0.2, friction = 0.3} -- Create new block. block = Block.new(blockImage, blockValue, blockFont, physicsWorld, blockBody, blockReleased) block:setPosition(posX, posY) stage:addChild(block) -- Add new sprite to the array. actors[blockBody] = block end --BLOCK.LUA part Block = Core.class(Sprite) function Block:init(image, value, font, world, body, released) -- Get the world. self.world = world -- Pass the the block body from main. self.body = body -- Joint with dummy body. self.mouseJoint = nil -- Create empty Box2D body for touches and mouse. self.dummyBody = self.world:createBody({}) self.released = released self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self) self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self) self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self) --self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) -- Add a label. label = TextField.new(font, value) -- Center the label on parent image. label:setPosition(image:getWidth() / 2 - label:getWidth() / 2 - 5, image:getHeight() / 2 + label:getHeight() / 2 + 15) -- Add label to the parent image. image:addChild(label) --image:setAnchorPoint(0.5, 0.5) -- Self is me, the sprite. self:addChild(image) return self end function Block:onMouseDown(event) if self:hitTestPoint(event.x, event.y) then local jointDef = b2.createMouseJointDef(self.dummyBody, self.body, event.x, event.y, 100000) self.mouseJoint = self.world:createJoint(jointDef) print("MOUSE_DOWN") --self.focus = true --self.x0 = event.x --self.y0 = event.y --event:stopPropagation() end end function Block:onMouseMove(event) if mouseJoint ~= nil then self.mouseJoint:setTarget(event.x, event.y) print("MOUSE_MOVE") end --if self.focus then --local dx = event.x - self.x0 --local dy = event.y - self.y0 --self:setX(self:getX() + dx) --self:setY(self:getY() + dy) --self.x0 = event.x --self.y0 = event.y --event:stopPropagation() --end end function Block:onMouseUp(event) if mouseJoint ~= nil then self.world:destroyJoint(mouseJoint) self.mouseJoint = nil print("MOUSE_UP") end --if self.focus then --self.focus = false --event:stopPropagation() --end end function Block:onEnterFrame (event) --physicsWorld:step(1/60, 8, 3) self:setPosition(self.body:getPosition()) end |
Dislikes: r033rt
Comments
You should be able to reference global variables, but that means than managing which order the code is executed. The larger the project that may be harder to manage, so keep that in mind.
In your onMouseUp and onMouseMove function you also need to do self.mouseJoint instead of just mouseJoint. Also, you may still want to have event:stopPropagation in your calls unless you want one mouse click/move to affect multiple objects.
Regards,
Marc
It is not clear why you cant use this within the class itself.
I also tried to create all the the bodies inside the class like
this:
Marc
"if self.mouseJoint ~= nil", sorry for that. The block object
will still not move.
Marc
have you checked this example:
http://appcodingeasy.com/Gideros-Mobile/Dragging-Box2d-object-in-Gideros-Mobile
Basically you need to perform a step in box2d world to make objects move, which is physicsWorld:step(1/60, 8, 3)
What exactly does this do? In Corona SDK, we have "Position Iterations" which define how accurate physics calculations are. Is this the same thing?
What is each number 1/60, 8, 3?
1/60 is something like 1 60th of a second, and 8 is and 3 is?
ng
Regards,
Marc
about 1/60, 8, 3
From box2d manual
http://www.box2d.org/manual.html#_Toc258082976
First is a time step to move world forward
float32 timeStep = 1.0f / 60.f;
Second is a quantity of velocity steps
int32 velocityIterations = 10;
Third is a quantity of position steps
int32 positionIterations = 8;