How would you do this (#29382)?

21 posts

 Just a quick one for you all to have an attempt at: The Problem You have a `2-dimensional array`, where there are `WH` number of Tiles. The number of tiles in the x-axis is `W`, and the number of tiles in the y-axis is `H`. The tiles are stored such that referencing the `2-d array` – which I will call `array` from now on – at `array[2][0]` would point to the tile at `X` position 0, and `Y` position (Notice the Y & X reference order may be backwards to what you might expect). So, in summary so far, you basically just have an array storing many tiles in different positions, with an easy method of accessing them directly if you know their `X` and `Y` values. Now, you must produce a solution which will tween the `alpha` value of each tiles from 0, to 1. The order must be like so; First, the very top left tile (AKA, the X:0, Y:0) will fade in first (in `fadeTime`). Once this has fully faded in (so once `fadeTime` has elapsed), the next tiles to fade in will be the tiles to the very right (AKA, the X:1, Y:0) and bottom (AKA, the X:0, Y:1) tiles which will do the same as above. Once these two tiles have finished fading in, then the tile to the right of these, and the bottom of these (Which may be the same tiles) will fade in. Example picture of the order tiles should fade in: http://i47.tinypic.com/fvdwyd.png Note, that the `W` and `H` values are not fixed, and your solution should account for variable values and length of the `array`, and not just hardcore for 1 `W` and `H` combination. EDIT: This is number #29382 because it just is, live with it… ```fadeList:Vector; fadeTile(x,y) { if(y = {...}; for(var i = 0; i < W; i++) { //arrays done in the forwards direction due to ease of //understanding of what it's doing for(var j = 0; j < H; i++) { Tweener.addTween(arr[j][i], {time:1, delay:i+j}); //the delay on any given tile //is equal to it's cardinal //distance (x-dist + y-dist) from //the origin } } ``` Originally posted by Draco18s: Pretty much identical to the approach I went with. ```for(var a:int=0;a d) { t.alpha = (g - d) / FADE_TIME if(g > (d + FADE_TIME)) t.removeEventListener(Event.ENTER_FRAME, fadeIn) } } } ``` Originally posted by DPbrad:Originally posted by qwerber:Why would you need a tween engine for something like this??? In terms of something simple like this, it isn’t massively necessary, just easier and quicker. However in terms of something much more complex, sequenced and confusing, using a Tween library is much easier than manually tracking 1000’s of objects animation and alpha states and changing accordingly. Au contraire. Tween engines are good for simple stuff like this; you can afford to hit performance in favour of readability when you’re only going to do something once, like a transition, or an `init` method, but you don’t want to kill performance by using a tween engine on thousands of objects in an important part of your application’s logic. There are a bunch of laggy games because of “hey, it’s easier” mentality. Generally I can be pretty aware of if what I’m using it for is going to have a frame rate impact or not. Or I can go “shit, frame rate is crap, what do I have that I don’t need?” and go “well, this is the only place I’m using tweener” and find another way. Originally posted by Senekis93:Originally posted by DPbrad:Originally posted by qwerber:Why would you need a tween engine for something like this??? In terms of something simple like this, it isn’t massively necessary, just easier and quicker. However in terms of something much more complex, sequenced and confusing, using a Tween library is much easier than manually tracking 1000’s of objects animation and alpha states and changing accordingly. Au contraire. Tween engines are good for simple stuff like this; you can afford to hit performance in favour of readability when you’re only going to do something once, like a transition, or an `init` method, but you don’t want to kill performance by using a tween engine on thousands of objects in an important part of your application’s logic. There are a bunch of laggy games because of “hey, it’s easier” mentality. I never said using a Tween engine would run faster, just a lot easier & quicker to code. Even with thousands of objects all having properties tweened around, the actual speed increase from doing things manually is pretty negligble (In my experience anyway). Note: Especially if you are using a well optimized tweening engine. If you run into performance issues, it might be better to only add one tween at a time ```private var cursorX:int = -1; private var cursorY:int = 0; private var tiles:Vector.; private function fade():void{ if(++cursorX >= tiles.length){ cursorX = 0; if(++cursorY >= tiles[0].length) return; } Tweenlite.to(tiles[cursorX][cursorY], DELAY, {alpha:1,onComplete:fade}); } ``` Personally, I almost always use 1D arrays for 2D data because it’s much easier to iterate through. If you need to access a 1D array as if it’s 2D you just do ```function getTile(X:int,Y:int):void{ return tiles[Y*WIDTH+X]; } ``` where WIDTH is the maximum x index. Yeah, I use the same setup Rivaledsouls in my own personal projects, but this was a project I inherited from elsewhere and all these type of arrays were in 2d format. If you’re running into performance issues due to graphical effects then you’re doing it wrong.