我正在尝试编写一个简单的电晕 SDK 游戏,但我遇到了一些问题。我的游戏中的绘图功能无法正常工作。您可以在点击开始按钮之前进行绘制。当你双击游戏崩溃。
main.lua 文件
display.setStatusBar( display.HiddenStatusBar )
--requiring libraries
local physics = require ("physics")
physics.start(true)
physics.setGravity(0,9)
--constants
local _W = display.contentWidth /2;
local _H = display.contentHeight /2;
-- game variables
local games = display.newGroup()
local orkHeight = 42;
local orkWidth = 40;
local score = 1;
local currentlevel;
local gameEvent = "";
local draw
--menu screen
local titleScreenGroup;
local titleScreen;
local playBtn;
--game screen
local background;
local goal;
local player;
local objective;
--Text box group
local objectiveText;
local levelText;
local levelNum;
-- textBoxGroup
local textBoxGroup;
local textBox;
local conditionDisplay;
local messageText;
local bx, by=0, 0 -- Create our variables for drawing the line
local lines={}
local p=0
local e=0
function main()
showTitleScreen();
end
function showTitleScreen()
--alle title elementen in een groep ;p
titleScreenGroup = display.newGroup();
--background
background = display.newImage("titleScreen.png")
background.x = _W
background.y = _H
--play button
playBtn = display.newImage("playButton.png")
playBtn.x = _W;
playBtn.y = _H + 50
playBtn.name = "loadGame"
--inserting
titleScreenGroup:insert(background)
titleScreenGroup:insert(playBtn)
--press button
playBtn:addEventListener("tap", loadGame)
end
--load actual game
function loadGame(event)
if event.target.name == "loadGame" then
transition.to(titleScreenGroup,{time = 0, alpha=0, onComplete =
initializeGameScreen});
playBtn:removeEventListener("tap", loadGame)
end
end
function initializeGameScreen()
whiteBackground = display.newRect(0, 0, 480, 320)
whiteBackground:setFillColor(255,255,255)
player = display.newImage("ork.png")
player.x = _W - 125
player.y = _H - 50
physics.addBody(player, "static", {density=0, bounce=.0, friction=.2})
--goto level 1 :)
changelevel1()
end
function changelevel1()
print("HOI")
whiteBackground:addEventListener("tap", startGame)
end
function startGame()
draw:addEventListener("touch", drawALine)
drawALine(event)
end
function drawALine(event)
if "began"==event.phase then
bx, by=event.x, event.y -- Store the values, if you don't it starts from 0, 0
elseif "moved"==event.phase then
lines[p]=display.newLine(bx, by, event.x, event.y) -- Make a normal line
--adding physics to the lines
physics.addBody(lines[p], "static", {density=.2, friction=.0, bounce=0});
--Width
lines[p].width=6 -- Just a boring old set width
--color
lines[p]:setColor(0,0,0)
bx, by=event.x, event.y -- Reset the bx and by, comment out for a "starburst" effect
p=p+1
e=e+1
elseif "ended"==phase then
end
end
main()
我知道图像和东西不合适,但我只是想制作一个简单的游戏。
提前致谢。