Two questions regarding this code.
1) Why doesn't the box rotate as it hits the platform?
2) What am I doing wrong that prevents applyForce from being applied each frame while the A button is being held down?
- Code: Select all
- MOAISim.openWindow("Rocket Test", 960, 480)
- STAGE_WIDTH = 960
- STAGE_HEIGHT = 480
- viewport = MOAIViewport.new()
- viewport:setScale (960,480)
- viewport:setSize(960,480)
- layer = MOAILayer2D.new()
- layer:setViewport(viewport)
- MOAISim.pushRenderPass(layer)
- engineA = {}
- engineA.on = false
- engineA.thrust = 2000
- physWorld = MOAIBox2DWorld.new()
- physWorld:setGravity(0, -1.8)
- physWorld:setUnitsToMeters(1/10)
- physWorld:start()
- layer:setBox2DWorld(physWorld)
- MOAIGfxDevice.setClearColor(1,1,1,1)
- staticBody = physWorld:addBody(MOAIBox2DBody.STATIC)
- staticBody:setTransform(0,-100,20)
- dynamicBody = physWorld:addBody(MOAIBox2DBody.DYNAMIC)
- dynamicBody:setTransform(-50,0)
- kinematicBody = physWorld:addBody(MOAIBox2DBody.KINEMATIC)
- kinematicBody:setTransform(50,0)
- rectFixture = staticBody:addRect(-200, -15, 200, 15)
- circleFixture = dynamicBody:addCircle(0,0,20)
- hexPoly = {
- -10, 20,
- -20, 0,
- -10, -20,
- 10, -20,
- 20, 0,
- 10, 20,
- }
- hexFixture = kinematicBody:addPolygon (hexPoly)
- shipBody = physWorld:addBody(MOAIBox2DBody.DYNAMIC)
- shipBody:setMassData(30)
- shipBody:setTransform(-200,-50)
- shipFixture = shipBody:addRect(-60,-20,60,20)
- forcesThread = MOAICoroutine.new()
- forcesThread:run (
- function()
- while true do
- if (engineA.on == true) then
- shipBody:applyForce(0,-2000,0,0)
- end
- coroutine.yield()
- end
- end
- )
- --input event callbacks
- if(MOAIInputMgr.device.keyboard) then
- MOAIInputMgr.device.keyboard:setCallback(
- function (key, down)
- if(down==true) then
- if (key=='a') then
- engineA.on = true
- end
- else
- if(key=='a') then
- engineA.on = false
- end
- end
- end
- )
- end