【问题标题】:Simple lua game with corona sdk使用 Corona sdk 的简单 lua 游戏
【发布时间】:2013-09-11 14:33:30
【问题描述】:

我正在尝试编写一个简单的电晕 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()

我知道图像和东西不合适,但我只是想制作一个简单的游戏。

提前致谢。

【问题讨论】:

    标签: sdk lua coronasdk


    【解决方案1】:

    在我看来,addEventListener 仅用于显示对象。在您的代码中没有绘制对象。所以代码如下: 删除这一行

    draw:addEventListener("touch", drawALine)

    替换

    Runtime:addEventListener("touch", drawALine)

    并删除此行drawALine(event)

    它对我有用。

    函数 startGame()
    运行时:addEventListener("touch", drawALine)
    --drawALine(事件)
    结束

    【讨论】:

      猜你喜欢
      • 2014-05-09
      • 2016-04-12
      • 2016-07-31
      • 2015-01-07
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      相关资源
      最近更新 更多