2012-01-31 45 views
0

試圖讓一個精靈(實例1)在觸摸時跳躍,但它不起作用。電暈:不能讓實例在屏幕上觸摸「跳躍」

這裏是我的代碼:

physics.addBody(instance1, { density=1.0, friction=0.3, bounce=0.3}) 
local function jump(event) 
    if(event.numTaps == 2) then 

    instance1:applyForce(350, -2000, instance1.x, instance1.y) 
    end 
end 

instance1:addEventListener("tap", jump) 

我會補充一點,如果我這樣做,精靈實例將跳一次,但從來沒有一次:

local function jump(event) 
    if(event.numTaps == 2) then 
    physics.addBody(instance1, { density=1.0, friction=0.3, bounce=0.3}) 
    instance1:applyForce(350, -2000, instance1.x, instance1.y) 
    end 
end 

instance1:addEventListener("tap", jump) 

實例信息:

local sheet1 = sprite.newSpriteSheet("character.png", 75, 105) 

local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 16) 

sprite.add(spriteSet1, "character", 1, 12, 700, 1) -- play 12 frames every 700 ms 

local instance1 = sprite.newSprite(spriteSet1) 

instance1.x = display.contentWidth/2 
instance1.y = 240 
+0

我們可以看到更多關於'instance1'的代碼嗎?當我假設'instance1'爲一個矩形時,我不能重現你的錯誤。 – cctan 2012-02-01 07:30:25

+0

@cctan:添加了intance1信息。 – kylex 2012-02-01 15:49:55

回答

0

仍然無法重現你的錯誤,
我回答你的問題到p我測試了代碼:

--main.lua 
local sprite=require("sprite") 
local physics=require("physics") 

local sheet1 = sprite.newSpriteSheet("character.png", 75, 105) 

local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 16) 

sprite.add(spriteSet1, "character", 1, 12, 700, 1) -- play 12 frames every 700 ms 

local instance1 = sprite.newSprite(spriteSet1) 

instance1.x = display.contentWidth/2 
instance1.y = 240 

physics.start() 
physics.setGravity(0,1) 
physics.addBody(instance1, { density=1.0, friction=0.3, bounce=0.3}) 
local function jump(event) 
    if(event.numTaps == 2) then 

    instance1:applyForce(35, -200, instance1.x, instance1.y) 
    end 
end 

instance1:addEventListener("tap", jump)