我在日冕运动时遇到问题。我已经设置好了,所以我的角色可以左右移动,但是现在我正试图这样做,当你点击他时他会跳起来。我无法让代码工作。
代码:
---------------------------------------------------------------------------------
-- CHARACTER PROPERTIES
---------------------------------------------------------------------------------
display.setDefault("magTextureFilter", "nearest")
local options =
{ --required parameters
width = 32,
height = 32,
numFrames = 20,
--optional parameters; used for dynamic resolution support
sheetContentWidth = 160, -- width of original 1x size of entire sheet
sheetContentHeight = 128 -- height of original 1x size of entire sheet
}
local sheetAsh = graphics.newImageSheet( "boy.png", options )
local sequenceData =
{
{ name="down_stand", start=1, count=1},
{ name="down_run", frames={2, 3, 4, 5}, time=400, loopCount=0, loopDirection = "bounce"},
{ name="left_stand", start=6, count=1},
{ name="left_run", frames={7, 8, 9, 10}, time=800, loopCount=0, loopDirection = "bounce"},
{ name="up_stand", start=11, count=1},
{ name="up_run", frames={12, 13, 14, 15}, time=1000, loopCount=0, loopDirection = "bounce"},
{ name="right_stand", start=16, count=1},
{ name="right_run", frames={17, 18, 19, 20}, time=800, loopCount=0, loopDirection = "bounce"},
}
local spriteAsh = display.newSprite( sheetAsh, sequenceData )
physics.addBody(spriteAsh, "dynamic", {density =1.5 , bounce = 0.5, radius = 28})
spriteAsh.x = _W * 0.5
spriteAsh.y = _H * 0.5
spriteAsh.xScale = 2
spriteAsh.yScale = 2
spriteAsh.linearDamping = 2
--spriteAsh.gravityScale = 0.1
--spriteAsh:setSequence( "down_run" )
--spriteAsh:play()
local touchX, touchY
local touchScreen = function(e)
--print(e.phase, e.x, e.y)
if e.phase == "began" then
touchX = e.x
touchY = e.y
elseif e.phase == "moved" then
--spriteAsh.x = spriteAsh.x + (e.x - touchX)
--spriteAsh.y = spriteAsh.y + (e.y - touchY)
local difX = e.x - touchX
local difY = e.y - touchY
spriteAsh:applyForce(difX *50, 0, spriteAsh.x, spriteAsh.y)
touchX = e.x
touchY = e.y
elseif e.phase == "ended" then
end
end
local function jumptouch( event )
if ( event.phase == "began" ) then
spriteAsh:applyForce( 250, -1500, spriteAsh.x, spriteAsh.y )
end
return true
end
local function onScreenTouch( event )
if event.phase == "began" then
-- make character jump forward
-- character:applyForce( 15000, -2500, character.x, character.y )
--if event.xStart == character.x then
spriteAsh:applyForce( 250, -1500, spriteAsh.x, spriteAsh.y )
--end
end
return true
end
spriteAsh:addEventListener( "tap", jumptouch, )
答案 0 :(得分:3)
“点按”事件没有event.phase字段。所以如果你删除event.phase ==“开始”检查,我认为它会起作用。