Корона: не могу заставить экземпляр "прыгать" на сенсорном экране - PullRequest
0 голосов
/ 31 января 2012

Попытка заставить спрайт (instance1) прыгать при касании, но это не сработает.

Вот мой код:

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

1 Ответ

0 голосов
/ 02 февраля 2012

все еще не могу воспроизвести вашу ошибку,
я ответил на ваш вопрос, чтобы вставить проверенный код:

--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) 
...