2014-11-04 120 views
0

這款遊戲就像殺死蚊子一樣。這是1級文件。當玩家點擊蜜蜂時,會顯示遊戲結束。當時間到了,遊戲結束會顯示。殺死的蚊子總數將顯示在最後。我對電暈和遊戲開發非常陌生。 我的問題是:如何添加一個按鈕返回主菜單(另一個.lua文件)?

  1. 在哪裏可以通過「觸摸」功能返回主菜單?它的代碼是什麼?
  2. 我正在使用作曲家。所以我嘗試了composer.gotoScene(「菜單」),但似乎並不奏效。錯誤。

在此先感謝您提供的任何幫助。下面是部分代碼:

local composer = require("composer") 
local scene = composer.newScene() 
local physics = require("physics") 
local widget = require "widget" 
physics.start() 
rand = math.random(20) 

local slap_sound = audio.loadSound("Sound/slap2.mp3") 
local ow = audio.loadSound("Sound/ow.mp3") 
local buttonSound = audio.loadSound("Sound/sound2.mp3") 
local back 

--local mossie_sound = audio.loadSound("Sound/mossie.mp3") 
local count={total1=0,total=0,touch=0,life=3} 

local background = display.newImageRect("Images/bg.jpg", display.contentWidth, display.contentHeight) 
    background.anchorX = 0 
    background.anchorY = 0 
    background.x, background.y = 0, 0 

local total=display.newText("Score : 0",display.contentWidth * 0.5, 20, "Arial", 26) 
     total:setTextColor(000000) 

local time_remain = 30 
local mossie 
local bee 
local shade 
local gameOverScreen 
local winScreen 
local gameIsActive = false 

local countdown=display.newText(time_remain ,display.contentWidth * 0.9, 20, "Arial", 26) 
countdown:setTextColor(000000) 

local life = display.newText("Life : 3 " ,display.contentWidth * 0.5, 50, "Arial", 26) 
life:setTextColor(000000) 

local pauseBtn = display.newImage("Images/pause.png") 
    pauseBtn.x = display.contentWidth * 0.1 
    pauseBtn.y = display.contentHeight - 450 

local resumeBtn = display.newImage("Images/playb.png") 
    resumeBtn.x = display.contentWidth * 0.1 
    resumeBtn.y = display.contentHeight - 450 


    local gameOver = function() 
    composer.removeScene("level1") //scene cannot be removed??? 
    gameIsActive = false 
    physics.pause() 
    gameOverScreen = display.newImage("Images/gameover.png",400,300) 
    gameOverScreen.x = 160 
    gameOverScreen.y = 240 
    gameOverScreen.alpha = 0 
    transition.to(gameOverScreen,{time=500,alpha=1}) 
    total.isVisible = true 
    total.text="Score : "..count.touch 
    total.x = 160 
    total.y = 400 
     botwall.isVisible = false 
     mossie.isVisible = false 
     bee.isVisible = false 
     life.isVisible = false 
     countdown.isVisible = false 
     pauseBtn.isVisible = false 
     resumeBtn.isVisible = false 

    end 



    local collisionListener=function(self,event) 
     if(event.phase=="began")then 
      if(event.other.type=="mossie")then 
       audio.play(ow) 
       count.life=count.life-1 
        if(count.life==0) then 
         gameOver() 
        end 
       event.other:removeSelf() 
       event.other=nil 
      else 
       event.other:removeSelf() 
       event.other=nil 
      end 

     end 

    end 


    local function countDown(e) 
     time_remain = time_remain-1 
     countdown.text = time_remain 
    end 

    local checkTimer = function() 
     if(time_remain == 0) then 
     gameOver() 
     end 
    end 

    function killIt(e) 
     if(e.phase == "ended") then 
      gameOver() 
     end 
    end 

    --spawn Bee 
    local function newBee(event) 

     bee = display.newImage("Images/lebah.png") 
     bee.x = 60 + math.random(160) 
     bee.y = -100 
     bee.type="other" 
     physics.addBody(bee, { density=1.4, friction=0.3, bounce=0.2}) 
     checkTimer() 

     bee:addEventListener("touch",killIt) 

    end 

    ---whenMossieIsTouched 
    function onTouch(mossie) 
     audio.play(slap_sound) 
     count.touch=count.touch+1 
     total.text="Score : "..count.touch 
     mossie.target:removeSelf() 
     --print("total"..mossietouchcount) 
    end 

    ---spawn Mossie 
    local function newMossie(event)  
     --audio.play(mossie_sound) 
     total.text="Score : "..count.touch 
     life.text="Life : "..count.life 
     mossie = display.newImage("Images/biasa.png") 
     mossie.x = 60 + math.random(160) 
     mossie.y = -100 
     mossie.type="mossie" 
     mossie:setFillColor(255,0,0) 
     physics.addBody(mossie, { density=0.3, friction=0.2, bounce=0.5}) 
     mossie.name = "mossie" 
     --checkTimer() 
     mossie:addEventListener("touch",onTouch) 

    end 

    local bottomWall = function() 
     --botwall=display.newRect(0,display.contentHeight,display.contentWidth*2,10) 
     botwall=display.newImage("Images/tangan.png") 
     botwall.x = 160 
     botwall.y = 500 
     botwall:setFillColor(22,125,185,255) 
     botwall.type="botwall" 
     botwall.collision=collisionListener 
     physics.addBody(botwall,"static",{ density=100.0, friction=0.0, bounce=0.0}) 
     botwall:addEventListener("collision",botwall) 
    end 

    local gameActivate = function() 
     gameIsActive = true 
    end 



    local gameStart = function() 
    local gametmr = timer.performWithDelay(1000, countDown, 0) 
    local dropMossie = timer.performWithDelay(1000 , newMossie, -1) 
    local dropBee = timer.performWithDelay(1800 , newBee, -1) 

    local pauseGame = function(e) 
     if(e.phase=="ended") then 
      audio.play(buttonSound) 
      physics.pause() 
      timer.pause(gametmr) 
      pauseBtn.isVisible = false 
      resumeBtn.isVisible = true 
      return true 
     end 
    end 

    local resumeGame = function(e) 
     if(e.phase=="ended") then 
      audio.play(buttonSound) 
      physics.start() 
      timer.resume(gametmr) 
      pauseBtn.isVisible = true 
      resumeBtn.isVisible = false 
      return true 
     end 
    end 

    pauseBtn:addEventListener("touch", pauseGame) 
    resumeBtn:addEventListener("touch", resumeGame) 
    resumeBtn.isVisible = false 
    bottomWall() 
    gameActivate() 


    end 

    gameStart() 
    return scene 

回答

0

確保一幕被稱爲「menu.lua」當您嘗試使用composer.gotoScene("menu")

首先確保您所需要的庫頂部

local composer = require("composer") 
local widget = require("widget") 

然後添加一個監聽器將處理按鈕事件

local function handleButtonEvent(event) 
    if ("ended" == event.phase) then 
     composer.gotoScene ("menu") 
    end 
end 

然後創建按鈕

local myButton = widget.newButton 
{ 
    left = 100, 
    top = 200, 
    id = "myButton", 
    label = "Default", 
    onEvent = handleButtonEvent 
} 

編輯: 看你的代碼,我可以你實際上並沒有使用作曲家看到。當然,你可能會包括這些庫,但是你沒有設置任何工作所需的功能。

我建議你閱讀這篇文章:Introducing the Composer API。這將引導您完成作曲家的使用。

至少你應該有以下功能讓作曲家工作。

  • 場景:創建()
  • 一幕:表演()
  • 場景:隱藏()
  • 場景:刪除()

並在那些使用利用場景的看法local sceneGroup = self.view。一旦物體被正確插入,您可以「管理」您的場景和其中的顯示對象。

+0

首先,非常感謝你的回覆。 所以,我現在關心的是我沒有想法把代碼放在哪裏.. 現在,我已經把所有的背景,蚊子,蜜蜂,timetext放在函數create中。我將它們插入到sceneGroup中,我添加了eventListener。 那麼,我應該在哪裏放置或如何保持產卵的蚊子和蜜蜂? 如何設置倒數計時器? 我應該把所有這些代碼放在哪裏? – 2014-11-05 03:10:04

+0

您可以將所有代碼放入創建中。唯一需要記住的是將顯示對象添加到sceneGroup/Scene視圖。 – 2014-11-05 13:37:01

相關問題