繁体   English   中英

如何在电晕sdk中为屏幕上创建的随机对象制作动画

[英]How to animate random objects created on screen in corona sdk

我试图在随机位置在屏幕上单独创建随机对象的动画,对象将在随机位置创建并向右移动,当它们越过屏幕宽度时,它们将从左侧(屏幕外)产生。 我无法理解如何在屏幕上为随机创建的对象设置动画。 以下是我使用的代码。 请帮忙。 谢谢....

--objects that are created randomly
local randoms=math.random
local randomx,randomy
local randomobjname1,randomobjname2

for i=1, 2 do
  randomx=randoms(200,400)
  randomy=randoms(600,800)
  local xlocation=randomx
  local ylocation=randomy

  local RandomObject[i]=display.newImage("object.png")
  RandomObject[i].x=xlocation
  RandomObject[i].y=ylocation

    if i==1 then
      randomobjname1=RandomObject[i]
    elseif i==2 then
      randomobjname2=RandomObject[i]
    end

  local function animateobj()
    --in this line i have confusion how to pass random x position that i got previously from the above function
    randomobjname1.x=randomx
    randomobjname2.x=randomx
    transition.to(randomobjname1,{time=1500,x=700, onComplete=animateobj})
    transition.to(randomobjname2,{time=1500,x=700, onComplete=animateobj})
  end
end

你在找这个:

local RandomObject = {}
local xPos = {}
local transitionTime = 1500

local listener2 = function( obj )
    transitionTime = 2000 -- U can select as ur need
    RandomObject[obj.tag].x = xPos[obj.tag]-400 -- U can even choose a difft. val than '400'
    animateobj(obj.tag)
end

function animateobj(i_)
    transition.to(RandomObject[i_],{time=transitionTime,x=400+xPos[i_], onComplete=listener2})
end

for i=1, 2 do
    RandomObject[i]=display.newImage("object.png")
    RandomObject[i].x = math.random(100,300)
    RandomObject[i].y = math.random(100,400)
    RandomObject[i].tag = i
    xPos[i] = RandomObject[i].x
    animateobj(i)
end

保持编码............ :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM