Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
move background — Gideros Forum

move background

MaxMax Member
edited May 2014 in General questions
Hello,
I have a question again.
I want, the background of my game to move down until you can't see it anymore.
But it shouldn't jump out of the screen, it should move. I need this for my game, because it should look like the player is flying. My problem is, that it didn't work with the event enter frame and the background should move without an event like mouse down.
I tried it, but it didn't work.
I hope someone can help me.
Thanks

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Here's what I'm using for horizontal movement:
    Scene = Core.class(Sprite)
     
    function Scene:init()
        self.roadSpeed = 2
        self.road1 = self:generateRoad()
        self.road2 = self:generateRoad()
        self.road2:setX(self.road1:getX() + self.road1:getWidth())
     
        self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end
     
    function Scene:generateRoad()
        local road = Bitmap.new(Texture.new("images/road.jpg", true))
        self:addChild(road)
        return road
    end
     
    function Scene:onEnterFrame()
        self.road1:setX(self.road1:getX()-self.roadSpeed)
        self.road2:setX(self.road2:getX()-self.roadSpeed)
        if(self.road1:getX() + self.road1:getWidth() <= 0) then
            self.road1:setX(self.road2:getX() + self.road2:getWidth())
        end
        if(self.road2:getX() + self.road2:getWidth() <= 0) then
            self.road2:setX(self.road1:getX() + self.road1:getWidth())
        end
    end
  • MaxMax Member
    Thanks for your answer :)
    Sorry, for my late answer :(
    I had no time answer (and I forgot it ;)) :(

    This works now, but I have a new problem.
    I wanted to make collision detection between two objects.
    I tried this:

    object1top = object1Image:getY()-object1image:getHeight*0.5
    object1down = object1Image:getY()+object1image:getHeight*0.5

    So I defined the top, down, left and right side of the object.

    And so on (I made the same with object2, too).

    Then I wanted to check if both objects are on collision with each other :

    if object1top <= object2top and (and so on.) then

    end

    But it doesn't work. I got the error, that I compair two nil values.
    If I use == instead of (for example) <=, then it works.


    I hope you can help me
    Thanks :)


  • MaxMax Member
    And I have another problem:
    The Textobject should display the score, but it doesn't update, so it is always 0.
    How can I update it?
    Thanks :)
  • ar2rsawseenar2rsawseen Maintainer
    @Max
    you can put this in init.lua (and create init.lua before if you don't have one)
    function Sprite:collidesWith(sprite2)
    	local x,y,w,h = self:getBounds(stage)
    	local x2,y2,w2,h2 = sprite2:getBounds(stage)
     
    	return not ((y+h < y2) or (y > y2+h2) or (x > x2+w2) or (x+w < x2))
    end
    And then Sprite and any Sprite inherited object will have a method collidesWith to test for collision with other sprite inherited objects ;)
  • ar2rsawseenar2rsawseen Maintainer
    Updating text object, you simply set it's text value to a new one, like this:
    local text = TextField.new(someFont, "0")
    text:setText("1")
  • MaxMax Member
    Thanks for your great help :)

    I don't really understand how the first code works and how I have to implement in my code.
    Thanks :)
  • ar2rsawseenar2rsawseen Maintainer
    You know what Sprite is?

    basically, create new file in Gideros Studio and call it init.lua
    And copy paste the code there, that will ensure that this code is loaded before any other.

    The code it self is a simply boundary check between two rectangles.

    Then for any object, you can simply use:
    local bmp1 = Bitmap.new(Texture.new("image1.png", true))
    local bmp2 = Bitmap.new(Texture.new("image2.png", true))
     
    if bmp1:collidesWith(bmp2) then
        --they collide, do something on collision <img class="emoji" src="http://forum.gideros.rocks/resources/emoji/wink.png" title=";)" alt=";)" height="20" />
    end
  • MaxMax Member
    I tried it now. I didn't get an error, but nothing happens if my two objects are on collision with each other :(
  • petecpetec Member
    Did you put anything in the
    if bmp1:collidesWith(bmp2) then
        --they collide, do something on collision 
    end
    bit to make something happen? Even just print("bump") would do to see if it's working.
  • MaxMax Member
    edited June 2014
    It works now :)
    But if object1 is only near object2, you lose the game.
    Can I make the boundaries of my objects smaller than they are?
    Thanks :)
Sign In or Register to comment.