2014-11-04 2 views
0

이 게임은 모기를 죽이는 것과 같습니다. 이것이 레벨 1 파일입니다. 플레이어가 꿀벌을 두 드릴 때 게임 오버가 표시됩니다. 시간이되면 게임 오버가 표시됩니다. 죽인 총 모기가 끝에 표시됩니다. 나는 코로나와 게임 개발에있어 매우 새롭다. 내 질문 :메인 메뉴 (다른 .lua 파일)로 돌아가려면 버튼을 어떻게 추가합니까?

  1. "터치"를 통해 주 메뉴로 돌아가려면 어디에서 기능을 추가 할 수 있습니까? 그것을위한 코드는 무엇입니까?
  2. 나는 작곡가를 사용하고 있습니다. 그래서 나는 composer.gotoScene ("menu")을 시도했지만 작동하지 않는 것 같습니다. 오류.

미리 도움을 주셔서 감사합니다.

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

가 있는지 확인하십시오 당신이 최고

local composer = require("composer") 
local widget = require("widget") 
에서 라이브러리를 필요로 확인 만드는 composer.gotoScene("menu")

먼저 사용하려고하면 장면이 "menu.lua"라고 다음은 코드의 일부입니다

그런 다음 버튼 이벤트를 처리하는 리스너를 추가

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를 함수 작성에 넣었습니다. 나는 그들을 sceneGroup에 삽입하고, 나는 eventListener를 추가했다. 그래서 모기 나 꿀벌을 어떻게 산란시켜야합니까? 카운트 다운 타이머를 설정하는 방법은 무엇입니까? 이 코드를 어디에 넣어야합니까? –

+0

모든 코드를 작성 안에 넣을 수 있습니다. 기억해야 할 것은 표시 객체를 장면 그룹/장면 뷰에 추가하는 것입니다. –

관련 문제