Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How do I make them move — Gideros Forum

How do I make them move

MinstrelMinstrel Member
edited June 2015 in General questions
Hello guys. For this post I'm moving away from all the insanely 'complex' pool stuff and focusing on something much, much simpler. Getting a layer of sprites to move across the screen. I have already configured the sprites in a layer I just need the code for moving them across the screen and bouncing when the furthest sprite hits the edge.
I have the sprites in the layer configured like this: (edge) 1 O O O O O O O O 1 (edge)
Oh and if you need to consult it here is the code for the layer and for 'spawning' the sprite.
Invader = Core.class(Sprite)
 
function Invader:init() 
 
local anim1 = {}
anim1[1] = Bitmap.new(Texture.new("invader2.png"))
anim1[2] = Bitmap.new(Texture.new("invader.png"))
 
local anim1 = MovieClip.new{
{1,20, anim1[1]},
{20,40, anim1[2]},
anim1[1]:setScale(0.3)
}
 
anim1:setPosition(0,-5)
self:addChild(anim1) 
anim1:setGotoAction(40, 1)
 
anim1:gotoAndPlay(1)
end
 
local maxInvaders  = 10
 
 
local invaders = {} 
invadersLayer = Sprite.new() 
stage:addChild(invadersLayer)
 
local paddx = 75 
for i=1, maxInvaders do 
invaders[i] = Invader.new()
invaders[i]:setX(paddx*i) 
invadersLayer:addChild(invaders[i]) 
invadersLayer:setScale(0.5)
invadersLayer:setPosition(0 ,25)
 
end
Also, if possible, can you tell me why you've told me to do what you've told me to do, as any one can copy and paste code, so I want to actually understand why I'm doing things.
Thanks for your time! :D

Comments

  • piepie Member
    edited June 2015 Accepted Answer
    @Minstrel what do you think it's "insanely complex" about the pool stuff? it's pretty straightforward :)

    you should already know how to move the invadersLayer: you're already moving sprites on screen (think about the bullets). You just need to set some limits (as you did on the ship).

    Somewhere at the beginning, where you set defaults, you need to set a default direction for the invadersLayer: this will help later, while checking the limits.
     layerstartDir = "LTR" --Left to right as starting direction
    in your gameloop
     
    local layerX = invadersLayer:getX() --get actual invadersLayer position
     
     if layerstartDir == "LTR" and layerX<= 100 then --100 is my choice, it's the x position where layer movement direction should revert when moving LTR
       invadersLayer:setX(layerX+1) -- where 1 is the pixel speed at which layer moves Left to right
     else --we reached the limit, change direction
    	layerstartDir = "RTL"  --right to left
     end	
     
     if layerstartDir == "RTL" and layerX>= -50 then -- -50 is my choice, it's the x position where layer direction should reverse at the left of the screen when moving RTL
       invadersLayer:setX(layerX-1) -- same as before, but negative
     else --same as before
    	layerstartDir = "LTR"  -- left to right
     end

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • Oh ok. What you've said here makes a lot more sense than what I did:
    invadersLayer:setPosition(getPosition()+5) :D

    Also I'm a newbie not realising I'd already done it :D
    BTW You may find pool stuff easy but for me it's hard -_-

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • Also I, again really appreciate you explaining it, as, as I said earlier anyone can copy and paste but not everyone takes the time to know what the code means.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.