我对这个都很陌生,这个项目中的一切都只是占位符和废料工作。你们中的一些人可能会从其他《电晕》教程中认出一些图像,但这只是为了帮助我做得更好,所以不要太挑剔。这是Corona模拟器的下载链接:http://www.mediafire.com/download/6a78bsewgwsiyp2/GooMan.rar
无论如何,这是我的问题。跳跃按钮一开始看起来不错。如果我按住它,角色就会不断跳跃。如果我放手,他就会停下来。如果我在按箭头键的同时按住jump键,他就会朝那个方向跳。然而,似乎如果我跳一次,然后在角色接触地面时再次按下跳跃按钮,他就不会跳了。会突然失去反应能力。我必须稍微暂停一下,等待角色完全落地,然后我才能再次成功跳跃。为什么?
这里是所有相关的代码供你查看:
我在开头有这个来帮助定义我的goo角色的位置:
local spriteInAir = false
local yspeed = 0
local oldypos = 0
local holding = false
然后创建跳转按钮
local bJump = display.newImage("Images/jumpbutton.png")
bJump.x = 445
bJump.y = 265
bJump.xScale = 0.78
bJump.yScale = 0.78
随后创建一个enterFrame Runtime Event,它将更新我的goo角色在y中的位置。
以60帧/秒的速度运行游戏。function checkSpeed()
yspeed = sprite.y - oldypos
oldypos = sprite.y
if yspeed == 0 then
spriteInAir = false
else
spriteInAir = true
end
end
Runtime:addEventListener( "enterFrame", checkSpeed )
然后是最重要的部分。创造了一个名为"hold"的功能,它告诉游戏不仅让我的goo角色跳跃,而且只要按住bJump按钮,我的goo角色就会一直跳跃。完美的工作。"跳跃"函数是一个监听保持函数的触摸事件,所有这些都是由监听跳跃函数的bJump按钮执行的。
local function hold()
if holding and spriteInAir == false then
sprite:applyForce( 0, -8, sprite.x, sprite.y )
sprite:setLinearVelocity(0, -350)
spriteInAir = true
return true
end
end
local function jumping( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
event.target.alpha = 0.6
Runtime:addEventListener( "enterFrame", hold )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
event.target.alpha = 1
Runtime:removeEventListener( "enterFrame", hold )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
spriteInAir = false
return true
end
end
return true
end
bJump:addEventListener("touch",jumping)
谁能帮我找出这个问题,我将不胜感激! 您正在使用速度检查来检测角色是否在地面上。在与Box2D碰撞后,它可能需要一段时间才能将其设置为零,所以更好的方法是使用碰撞传感器:
sprite.grounded = 0 -- Use a number to detect if the sprite is on the ground; a Boolean fails if the sprite hits two ground tiles at once
function sprite:collision(event)
if "began" == event.phase then
-- If the other object is a ground object and the character is above it...
if event.other.isGround and self.contentBounds.yMax < event.other.contentBounds.yMin + 5 then
sprite.grounded = sprite.grounded + 1 -- ...register a ground collision
end
elseif "ended" == event.phase then
-- If the other object is a ground object and the character is above it...
if event.other.isGround and self.contentBounds.yMax < event.other.contentBounds.yMin + 5 then
sprite.grounded = sprite.grounded - 1 -- ...unregister a ground collision
end
end
end
sprite:addEventListener("collision")
然后,在您的跳转函数中,只需检查sprite.grounded > 0
。如果是,玩家被禁足。