2013-07-10 15 views
1

안녕하세요, 저는 Corona SDK 세계에서 처음입니다. 일부 개체를 생성하고 화면에서 움직이게하는 방법을 배우고 싶습니다. 모든 것을 시도하면 작동하지 않습니다. 오른쪽에서 산란에 관한 포럼을 읽습니다. 방법과 그것을 시도하지만 여전히 내 코드의 도움에 오류가 결국이 또한 corona sdk에서 개체를 생성하는 방법

local mRandom = math.random 
local mAbs = math.abs 
local objects = {"rocket02" ,"rocket01","coin01"} 

local function spawnObject() 
    local objIdx = mRandom(#objects) 
    local objName = objects[objIdx] 
    local object = display.newImage("image/object_"..objName..".png") 
    object.x = mRandom (screenLeft +30,screenRight-30) 
    object.y = screenTop 

    if objIdx < 4 then 
     object.type = "food" 
    else 
     object.type = "other" 
    end 
end 

어떤 사람이 어떻게

도와주세요 화면을 가로 질러 이동하게하는 감사

말해 줄 수 내 코드입니다

여기에 당신을위한 미디어 파일이 있습니다

답변

0

나는 당신에게 방법을 보여줄 것이다. 여기

local mRandom = math.random 
local objects = {"rocket02" ,"rocket01","coin01"} 
local objectTag = 0 
local object = {} 

local function spawnObject() 
    objectTag = objectTag + 1 
    local objIdx = mRandom(#objects) 
    local objName = objects[objIdx] 
    object[objectTag] = display.newImage(objName..".png") -- see the difference here 
    object[objectTag].x = 30+mRandom(320) 
    object[objectTag].y = 200 
    object[objectTag].name = objectTag 
    print(objectTag) 
end 
timer.performWithDelay(1,spawnObject,3) 

내가 객체를 표시하는 timer을 사용했습니다 다음과 같이 그 동안, 난 당신의 코드를 다시 작성했다. 같은 목적으로 for 루프를 사용할 수도 있습니다. 태그가있는 객체를 object[objectTag]과 같이 호출 할 수 있습니다. 당신의 내용

:

display.newImage(objName..".png") 
    --[[ will display object named rocket02.png or rocket01.png or coin01.png 
     placed in the same folder where your main.lua resides --]] 

그리고

display.newImage("image/object_"..objName..".png") 
    --[[ will display object named object_rocket02.png or object_rocket01.png 
     or object_coin01.png placed in a folder named 'image'. And the folder 
     'image' should reside in the same folder where your main.lua is. --]] 

그리고 위에서 아래로 개체를 이동, 당신은 사용할 수 있습니다

중 하나

function moveDown() 
    object[objectTag].y = object[objectTag].y + 10 
    --replace 'objectTag' in above line with desired number (ir., 1 or 2 or 3) 
end 
timer.performWithDelay(100,moveDown,-1) 

또는

transition.to(object[objectTag],{time=1000,y=480}) 
--[[ replace 'objectTag' in above line with desired number (ir., 1 or 2 or 3) 
eg: transition.to(object[1],{time=1000,y=480}) --]] 

.............. : 코딩 유지

+0

이봐, 난이 새로운 오류 메시지가 테이블 인덱스를 얻을 수 당신의 도움을 주셔서 감사합니다 – SeanDp32

+0

죄송합니다, 실수를 저지른 라인 객체 [i] = display.newImage ("image/object _".. objName .. "png")에 nil입니다. 'i'를 'objectTag'로 바꿉니다. 나는 위의 코드를 수정했습니다 ... –

+0

괜찮아요. 당신이 버그를 의미하는 것은 아니지만, 코드를 실행할 때 새로운 오류가 발생하는 것을 당신이 도와 주니 기쁘군요. 오류는 "개체를 인덱스 필드에 시도"라고 말합니다. [objectTag] .x = 30 + mRandom (320)이 부분을 숨길 수있는 내용을 공유하지 않습니다. 나는 당신이 내가이 문제를 해결할 수 있도록 내가이 문제를 해결할 수 있기를 바라고있다. – SeanDp32

관련 문제