Reputation: 404
I'm trying to write a simple corona SDK game but i have some problems. The draw function in my game doesn't work properly. You can draw before you tap the start button. And when you double tap the game crashes.
the main.lua file
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()
I know the images and stuff don't fit well, but i'm just trying to make a simple game.
Thanks in advance.
Upvotes: 0
Views: 668
Reputation: 267
In my opinion,addEventListener only for display object.In your code no draw object.so code like this: remove this line
draw:addEventListener("touch", drawALine)
replace
Runtime:addEventListener("touch", drawALine)
and remove this line drawALine(event)
it works for me.
function startGame()
Runtime:addEventListener("touch", drawALine)
--drawALine(event)
end
Upvotes: 2