用Corona SDK简单制作的Lua游戏

我尝试编写一个简单的Corona SDK游戏,但是我遇到了一些问题。我的游戏中的绘图功能无法正常工作。您可以在点击开始按钮之前进行绘制。双击后游戏会崩溃。

main.lua文件

display.setStatusBar(display.HiddenStatusBar)

--需要的库
local physics = require("physics")
physics.start(true)
physics.setGravity(0,9)

--常量
local _W = display.contentWidth / 2;
local _H = display.contentHeight / 2;

--游戏变量
local games = display.newGroup()
local orkHeight = 42;
local orkWidth = 40;
local score = 1;
local currentlevel;
local gameEvent = "";
local draw

--菜单屏幕
local titleScreenGroup;
local titleScreen;
local playBtn;

--游戏屏幕
local background;
local goal;
local player;
local objective;

--文本框组
local objectiveText;
local levelText;
local levelNum;

--textBoxGroup
local textBoxGroup;
local textBox;
local conditionDisplay;
local messageText;

local bx, by = 0, 0 --创建绘制线的变量
local lines={}

local p = 0
local e = 0

function main()
    showTitleScreen()
end

function showTitleScreen()
    --将所有标题元素放入组中;p
    titleScreenGroup = display.newGroup()

    --背景
    background = display.newImage("titleScreen.png")
    background.x = _W
    background.y = _H

    --播放按钮
    playBtn = display.newImage("playButton.png")
    playBtn.x = _W;
    playBtn.y = _H + 50
    playBtn.name = "loadGame"

    --插入
    titleScreenGroup:insert(background)
    titleScreenGroup:insert(playBtn)

    --按按钮
    playBtn:addEventListener("tap", loadGame)
end

--加载实际游戏
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})

    --转到1级:
    changelevel1()
end

function changelevel1()
    print("你好")
    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 --存储值,如果不这样做,它将从0,0开始
    elseif "moved" == event.phase then
        lines[p] = display.newLine(bx, by, event.x, event.y) --制作普通的线条
        --向线添加物理属性
        physics.addBody(lines[p], "static", {density = .2, friction = .0, bounce = 0});
        --宽度
        lines[p].width = 6 --只是一个无聊的旧宽度设置
        --颜色
        lines[p]:setColor(0, 0, 0)
        bx, by = event.x, event.y --重置bx和by,取消注释以获得“星形”效果
        p = p + 1
        e = e + 1
    elseif "ended" == phase then
        --结束
    end
end

main()

我知道图像等不合适,但我只是尝试制作一个简单的游戏。

提前致谢。

点赞
用户2469861
用户2469861

我认为,addEventListener 只适用于显示对象。在你的代码中没有绘制对象,因此代码应该像这样:

删除这行代码:

draw:addEventListener("touch", drawALine)

用以下代码替换:

Runtime:addEventListener("touch", drawALine)

并删除这行代码drawALine(event)

这对我有效。

function startGame()

Runtime:addEventListener("touch", drawALine)

--drawALine(event)

end

2013-09-12 07:21:59