Hintergrund Z-index / Scene-Dimensions

  • #10, by sebastianSaturday, 04. November 2017, 23:18 3 years ago
    Folgendes (ich schreibs mal in Englisch, damit auch ggf. andere Leute hier im Forum helfen können):



    Scene: Background of the scene: Black, used to dimensionate the scene,
    Object1: "animated_bg", containing 3 objekt animations: 
        1: "tunnel0", 1 Frame with image 1 , Loops [x], infinite , 
        2: "tunnel1" , 1 Frame with image 2 , Loops [x], infinite ,
        3: "tunnel2", 1 Frame with image3 , Loops [x], infinite ,
    Object2: train background
    Object3: train foreground

    The animations from object 1 can be put stacked outside of the viewable scene. These animations will be moved via Lua.

    SCENE ACTIONS:
    ------

    At beginning of Scene Action:
    Show animation: tunnel0
    Show animation: tunnel1
    Show animation: tunnel2
    Call other Action: "movebackground" (will be created below)
    (there is also an earthquake action part which is maybe usefull in the lowest setting to simulate the train jiggle)
    -------

    Called by other Action "movebackground":

    Actiopart #1 : Execute a script:
    --reset animations in 0ms to pos 0/0 
    ActiveAnimations["tunnel0"]:to(0, { VAnimationCurrentPosition = {x = 0, y = 0} }) 
    ActiveAnimations["tunnel1"]:to(0, { VAnimationCurrentPosition = {x = 0, y = 0} }) 
    ActiveAnimations["tunnel2"]:to(0, { VAnimationCurrentPosition = {x = 0, y = 0} })  
    --randomize a number between 0 and 2
    local randomtunnel = math.random(0,2)
    --move random tunnel animation to other side of the screen in 500ms (assuming x=2000 is outside the view)
    ActiveAnimations["tunnel"..randomtunnel]:to(500, { VAnimationCurrentPosition = {x = 2000, y = 0} }) 
    1
    2
    3
    4
    5
    6
    7
    8
    Actionpart #2: Pause 500ms
    Actionpart #3 Jump to Actionpart #1



    This should randomly move one animation from tunnel0-2 from 0/0 to 2000/0 in 500ms and looping that stuff.
    You should consider that it may looks better if your "real" background is entirely black and your tunnel0-2 animation images are fading into black at the left/right edges, Then you can do a bigger pause, so that the tunnel images appear in greater distance.

    ------
    ADDITIONAL SCENE ACTION to quit all processes which are relevant.
    At end of Scene Action:
    Hide animation: tunnel0
    Hide animation: tunnel1
    Hide animation: tunnel2
    Quit action: "movebackground"


    ------
    (untested)

    kind regards
    Sebastian



    Thread Captain

    2340 Posts


  • #11, by rhavin grobertSunday, 05. November 2017, 01:27 3 years ago
    Thanks, this is going to get in the right direction  grin

    Now, I did set up things like this:

    5 animations:

    "tunnel0a" -> tunnel0.jpg
    "tunnel0b" -> tunnel0.jpg
    "tunnel1a" -> tunnel1.jpg
    "tunnel1b" -> tunnel1.jpg
    "tunnel2" -> tunne2.jpg

    scUW = {}
    scUW.tilewidth = 913 -- width of a tiles image
    scUW.numtiles = math.ceil(game.WindowResolution.x / scUW.tilewidth)
    scUW.tiles0 = {"tunnel0a","tunnel0b"} -- animations with image0
    scUW.tiles1 = {"tunnel1a","tunnel1b"} -- animation with iamge1
    scUW.tiles2 = {"tunnel2"} -- animations with image2
    scUW.offset = 0
    scUW.ordering = {"tunnel0a","tunnel0b","tunnel2"}

    --[[ doesnt move, just for testing ]]--
    scUW.setBackground = function(self)
      local pos = 0
      for _,t in ipairs(self.ordering) do
        ActiveAnimations[t]:to(0, { VAnimationCurrentPosition =
        {x = self.offset + self.tilewidth * pos, y = 0} })
        pos = pos + 1
      end
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    The problem with this is, that even if I define 2 animations for the same image, it just draws one, so with

    scUW.ordering = {"tunnel0a","tunnel1a","tunnel2"} 
    1

    …I get all three adjacent, but with…

    scUW.ordering = {"tunnel0a","tunnel1a","tunnel0b"} 
    1

    …the 3rd one is missing. Do I really have to dublicate the file just to get 2 anis with the same image twice???

    Newbie

    47 Posts

  • #12, by sebastianSunday, 05. November 2017, 01:54 3 years ago
    the linked image should not be the problem. The animation name is different and that should be enough to differentiate. Is the missing animation visible and looping?
    Can you put in a "print" and see in the log if the for loop outputs anything for the third round? 

    Thread Captain

    2340 Posts

  • #13, by rhavin grobertSunday, 05. November 2017, 02:57 3 years ago
    Found it, I forgot to start it grin
      local pos = 0
      for _,t in ipairs(self.active) do
        local ani = getObject("Animations["..t.."]")
        startAnimation(ani)
        ActiveAnimations[t]:to(0, { VAnimationCurrentPosition =
          {x = self.offset + self.tilewidth * pos, y = 0} })
        pos = pos + 1
      end
    1
    2
    3
    4
    5
    6
    7
    8

    Can you please explain this part:

    VAnimationCurrentPosition

    Where does this 'V' come from? according to manual, its AnimationCurrentPosition. Also, why cant I do…

    ActiveAnimations[t].AnimationCurrentPosition = {…}?
    1

    …as it is timed *now*, but have to dispatch it via this to-method?

    Newbie

    47 Posts

  • #14, by sebastianSunday, 05. November 2017, 03:10 3 years ago
    the V is explained here:

    Im not sure if its still needed. The possibilities using shorter ways in the object declaration is still new to me. 

    You should also be able to do 
    ani.CurrentPosition =...  if you work with instant changes. The :to function is used to change values over time (with easing):

    Thread Captain

    2340 Posts

  • #15, by afrlmeSunday, 05. November 2017, 03:24 3 years ago
    V is not needed but I believe Animation is. Certain things work ok with short shorthand whereas some need the full field name without the V; animation being one of them to my knowledge.

    Imperator

    6862 Posts

  • #16, by rhavin grobertMonday, 06. November 2017, 00:13 3 years ago
    Getting closer. Now the background moves as expected, but some tiles move in the wrong direction. Seems like calling 

    ActiveAnimations[t]:to(0, { VAnimationCurrentPosition =
       {x = self.tilewidth * pos, y = 0} })
    ActiveAnimations[t]:to(self.tick, { VAnimationCurrentPosition =
       {x = self.tilewidth * (pos - 1), y = 0} })
    1
    2
    3
    4

    is the problem. let me explain:

    In the "class" I wrote, there is an array ordering that holds the actual shown tiles. The function orderBackground moves the zeroth element of the array outside the screen and moves all other elements one step down in index, adding new ones to the end of the array.
    That works. Its just the function moveBackground. It should place all tiles where they belong and then issue a move animation until the next tick.

    But when a tile, that just moved outside the screen is needed at the other end of the viewport, it doesent zero-delay jump but instead moves slowly in the opposite direction as if the first call to the 'to'-function just got ignored.

    Is there a way to just place the animation *now* without delaying it with 'to'?

    Full code below:

    math.randomseed( os.time() )

    -- unfuck luas base-1 bullshit
    function iarray(a)
      local n = 0
      local s = #a
      if a[0] ~= nil then
        n = -1
      end
      return function()
        n = n + 1
        if n <;= s then return n, a[n] end 
        return nil,nil
      end
    end


    QP_createTiledBackground = function(tilewidth, tick, aBasetiles, aAlt1, aAlt2, chance2)
      ret = {}

      ret.scWidth = game.CurrentScene.Sprite:getSize().x
      ret.numTiles = math.ceil(ret.scWidth / tilewidth)
      ret.offset = 0
      ret.tilewidth = tilewidth
      ret.chance2 = chance2 * 100
      ret.isInit = false
      ret.tick = tick
      ret.tiles = {}
      ret.ordering = {}
      local pos = 0
      for _,t in iarray(aBasetiles) do
        ret.tiles[pos] = t
        pos = pos + 1
      end
      ret.numBases = pos
      for _,t in iarray(aAlt1) do
        ret.tiles[pos] = t
        pos = pos + 1
      end
      ret.startAlt2 = pos
      ret.numAlt1 = pos - ret.numBases
      for _,t in iarray(aAlt2) do
        ret.tiles[pos] = t
        pos = pos + 1
      end
      ret.numAlt2 = pos - ret.numBases - ret.numAlt1

     -- move all animation-tiles outsize the viewport
      ret.initBackground = function(self)
        if (self.isInit) then
          return end
        self.isInit = true
        self.offset = 0
        for _,t in iarray(self.tiles) do
          local ani = getObject("Animations["..t.."]")
          startAnimation(ani)
          print (t)
          ActiveAnimations[t]:to(0, { AnimationCurrentPosition =
            {x = self.scWidth, y = 0} })
        end
      end

      -- check if a tile is already used in the ordering, skipping it
      ret.findTile = function(self, tile)
        for _,t in iarray(self.ordering) do
          if (t == tile) then return _ end
        end
        return -1
      end

      -- return an appropiate tile from given range or nil if none available
      ret.getNextTile = function(self, start, stop)
        local pos = start
        while (pos = math.random(0,100)) then
                self.ordering[pos] = self:getNextTile(self.startAlt2,#(self.tiles)+1)
              else
                self.ordering[pos] = self:getNextTile(self.numBases, self.startAlt2)
              end
            end
          end
          off = off + 1
          if (self.ordering[pos] ~= nil) then
            pos = pos + 1
          end
          if (off >; 20) then break end -- reasonable way out if to few tiles given
        end
      end

      ret.moveBackground = function(self)
        self:initBackground()
        self:orderBackground()
        local pos = 0
        for _,t in iarray(self.ordering) do
          ActiveAnimations[t]:to(0, { VAnimationCurrentPosition =
            {x = self.tilewidth * pos, y = 0} })
          ActiveAnimations[t]:to(self.tick, { VAnimationCurrentPosition =
            {x = self.tilewidth * (pos - 1), y = 0} })
          pos = pos + 1
        end
      end

      return ret
    end

    scUW = QP_createTiledBackground(913, 1500, {"tunnel0a","tunnel0b"}, {"tunnel1a","tunnel1b"}, {"tunnel2"}, 0.5)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105

    Newbie

    47 Posts

  • #17, by sebastianMonday, 06. November 2017, 00:27 3 years ago
    man, you are overcomplicating the whole stuff with huge amount of Lua which isnt really needed to achieve the effect you want.

    The :to() function is used to move stuff /change values over greater distances over time. When you want to move it only one pixel it looses its purpose. 

    Then you may change it directly via:

    ActiveAnimations[t].CurrentPosition = {x=something, y=something} 

    this should also answer your question smile 

    ~Sebastian 

    Thread Captain

    2340 Posts

  • #18, by rhavin grobertMonday, 06. November 2017, 00:32 3 years ago
    A (hopefully) working project attached:

    Newbie

    47 Posts

  • #19, by rhavin grobertMonday, 06. November 2017, 00:39 3 years ago
    ActiveAnimations[t].CurrentPosition = {x=something, y=something} 

    That doesnt do anything. BTW, you misinterpreted the code, i dont move it pixelwise, a tick is 1500 ms and in that time all tiles should move one tilewidth. Its just that the zero-delay-commands sometimes seem to get ignored.


    EDIT:

    My goodness, who's in charge for that stupid forum? some parts of my code are plainly not what I wrote but some wired copies from other parts of it just because of a less-than or greater-than in the code. Never heard of JavaScript html-escapement for text-areas?

    Newbie

    47 Posts

  • #20, by sebastianMonday, 06. November 2017, 00:47 3 years ago
    could also be this: 

    ActiveAnimations[t].AnimationCurrentPosition = {x=something, y=something} 

    see:

    its late. going to bed now. 
    gn8

    Thread Captain

    2340 Posts

Write post