电晕重力故障
Corona Gravity Glitch
我正在使用利用物理引擎的 Corona SDK 制作游戏,并且遇到了一个非常烦人的故障:每次我撞到小行星并按下后退按钮,再次按下播放键,绿色的家伙跳得更高值。
我将不胜感激!
这是我的代码:
主文件:
local Physics = require("physics")
Physics.start()
centerX = display.contentCenterX
centerY = display.contentCenterY
myScene = require("scene")
myHero = require("hero")
myGoomba = require("badguy")
myButtons = require("buttons")
myMenu = require("mainmenu")
myMenu.drawMainMenu()
playButton:addEventListener( "tap", myMenu.play )
主菜单:
local mainmenu = {}
local Physics = require("physics")
Physics.start()
function mainmenu.drawMainMenu()
menuTheme = audio.loadStream("Music/menuTheme.ogg")
menuThemeChannel = audio.play( menuTheme, { channel=1, loops=-1, fadein=3000 } )
background = display.newImage("menuBackground.png")
background.x = display.contentCenterX
background.y = display.contentCenterY
background:scale(1.2, 1.2)
playButton = display.newImage("playButton.png")
playButton.x = display.contentCenterX
playButton.y = display.contentCenterY - 75
playButton:scale( .3, .3 )
end
function mainmenu.play()
print("mainmenu - tap registered, preparing to load game..")
audio.stop(1)
myScene.sayHello()
myScene.drawScene()
myHero.sayHello()
myHero.drawHero()
myGoomba.hello()
myGoomba.drawBadGuy()
myButtons.sayHello()
myButtons.drawButtons()
myGoomba.move()
gameTheme = audio.loadStream("Music/playTheme.ogg")
gameThemeChannel = audio.play( gameTheme, { channel=2, loops=-1, fadein=0 } )
jumpButton:addEventListener("tap", myButtons.jump)
backButton:addEventListener("tap", myButtons.goBack)
end
function mainmenu.sayHello()
print("mainmenu - Hello....")
end
return mailmen
按钮:
local buttons = {}
local Physics = require("physics")
Physics.start()
function buttons.drawButtons()
jumpButton = display.newImage( "button.png")
jumpButton.x = 473
jumpButton.y = 305
jumpButton:scale( .2, .7 )
slideButton = display.newImage("button.png")
slideButton.x = 5
slideButton.y = 305
slideButton:scale(.2, .7)
backButton = display.newImage("backButton.png")
backButton.x = display.contentCenterX - 245
backButton.y = display.contentCenterY - 100
backButton:scale(.11, .11)
end
function buttons.jump()
--work on decreasing air time by increasing gravity...
instance2:applyLinearImpulse(0, -0.12, instance2.x, instance2.y)
--Applying 0 force in the X direction and -0.12 in the Y direction. Y is negative because
--down is considered positive by the physics engine.
--Apply force to the center of the green guy, hence instance.x & instance.y
end
function buttons.goBack()
print("buttons - loading main menu...")
audio.stop(2)
myMenu.drawMainMenu()
--reset gravity and jump-impulse values here
end
function buttons.sayHello()
print("buttons - Hello....")
end
return buttons
英雄:
local hero = {}
local Physics = require("physics")
Physics.start()
function hero.drawHero()
print("hero - loading hero...")
greenGuysheet = graphics.newImageSheet( "greenman.png", { width=128, height=128, numFrames=15 } )
-- play 15 frames every 500 ms
instance2 = display.newSprite( greenGuysheet, {start=1, count=15, time=500 } )
instance2.name = "greenGuy"
Physics.addBody(instance2, "dynamic", {radius = 20, bounce = 0})
instance2.x =100
instance2.y = 260
instance2.gravityScale = 2.5
instance2:play()
end
function hero.sayHello()
print("hero - Hello....")
end
return hero
您似乎没有使用 composer module. It is recommended to use it to manage scenes. Read more about it in tutorial Introducing the Composer API。
我不确定,但您似乎正在添加事件侦听器
jumpButton:addEventListener("tap", myButtons.jump)
backButton:addEventListener("tap", myButtons.goBack)
每次调用 mainmenu.play
函数。在你回到游戏并点击跳跃按钮后,监听器被调用两次。因此,您需要在转到菜单之前删除点击监听器或仅添加一次点击监听器。在 Corona documentation.
上阅读有关事件侦听器的更多信息
我正在使用利用物理引擎的 Corona SDK 制作游戏,并且遇到了一个非常烦人的故障:每次我撞到小行星并按下后退按钮,再次按下播放键,绿色的家伙跳得更高值。
我将不胜感激!
这是我的代码:
主文件:
local Physics = require("physics")
Physics.start()
centerX = display.contentCenterX
centerY = display.contentCenterY
myScene = require("scene")
myHero = require("hero")
myGoomba = require("badguy")
myButtons = require("buttons")
myMenu = require("mainmenu")
myMenu.drawMainMenu()
playButton:addEventListener( "tap", myMenu.play )
主菜单:
local mainmenu = {}
local Physics = require("physics")
Physics.start()
function mainmenu.drawMainMenu()
menuTheme = audio.loadStream("Music/menuTheme.ogg")
menuThemeChannel = audio.play( menuTheme, { channel=1, loops=-1, fadein=3000 } )
background = display.newImage("menuBackground.png")
background.x = display.contentCenterX
background.y = display.contentCenterY
background:scale(1.2, 1.2)
playButton = display.newImage("playButton.png")
playButton.x = display.contentCenterX
playButton.y = display.contentCenterY - 75
playButton:scale( .3, .3 )
end
function mainmenu.play()
print("mainmenu - tap registered, preparing to load game..")
audio.stop(1)
myScene.sayHello()
myScene.drawScene()
myHero.sayHello()
myHero.drawHero()
myGoomba.hello()
myGoomba.drawBadGuy()
myButtons.sayHello()
myButtons.drawButtons()
myGoomba.move()
gameTheme = audio.loadStream("Music/playTheme.ogg")
gameThemeChannel = audio.play( gameTheme, { channel=2, loops=-1, fadein=0 } )
jumpButton:addEventListener("tap", myButtons.jump)
backButton:addEventListener("tap", myButtons.goBack)
end
function mainmenu.sayHello()
print("mainmenu - Hello....")
end
return mailmen
按钮:
local buttons = {}
local Physics = require("physics")
Physics.start()
function buttons.drawButtons()
jumpButton = display.newImage( "button.png")
jumpButton.x = 473
jumpButton.y = 305
jumpButton:scale( .2, .7 )
slideButton = display.newImage("button.png")
slideButton.x = 5
slideButton.y = 305
slideButton:scale(.2, .7)
backButton = display.newImage("backButton.png")
backButton.x = display.contentCenterX - 245
backButton.y = display.contentCenterY - 100
backButton:scale(.11, .11)
end
function buttons.jump()
--work on decreasing air time by increasing gravity...
instance2:applyLinearImpulse(0, -0.12, instance2.x, instance2.y)
--Applying 0 force in the X direction and -0.12 in the Y direction. Y is negative because
--down is considered positive by the physics engine.
--Apply force to the center of the green guy, hence instance.x & instance.y
end
function buttons.goBack()
print("buttons - loading main menu...")
audio.stop(2)
myMenu.drawMainMenu()
--reset gravity and jump-impulse values here
end
function buttons.sayHello()
print("buttons - Hello....")
end
return buttons
英雄:
local hero = {}
local Physics = require("physics")
Physics.start()
function hero.drawHero()
print("hero - loading hero...")
greenGuysheet = graphics.newImageSheet( "greenman.png", { width=128, height=128, numFrames=15 } )
-- play 15 frames every 500 ms
instance2 = display.newSprite( greenGuysheet, {start=1, count=15, time=500 } )
instance2.name = "greenGuy"
Physics.addBody(instance2, "dynamic", {radius = 20, bounce = 0})
instance2.x =100
instance2.y = 260
instance2.gravityScale = 2.5
instance2:play()
end
function hero.sayHello()
print("hero - Hello....")
end
return hero
您似乎没有使用 composer module. It is recommended to use it to manage scenes. Read more about it in tutorial Introducing the Composer API。
我不确定,但您似乎正在添加事件侦听器
jumpButton:addEventListener("tap", myButtons.jump)
backButton:addEventListener("tap", myButtons.goBack)
每次调用 mainmenu.play
函数。在你回到游戏并点击跳跃按钮后,监听器被调用两次。因此,您需要在转到菜单之前删除点击监听器或仅添加一次点击监听器。在 Corona documentation.