Jump to content


Tower defence Tweening usage help

Recommended Posts

I am currently working on a 2player tower defence Game project and have put off this problem for a while and have given up trying to figure it out alone.


Firstly let me give you an example of the pseudoCode i'm working with and then i'll explain my problem in more detail

(I can put up the actual code if anybody has any questions)



for each creature {

for each waypoint {

//tween x value to waypoint x value

// onCompletion tween y value to waypoint y value





As you can see, the problem with this is that each creature begins his journey after the last has completed. I need a way to sequence the first two moves for each creature but wrap that in an overall movement with an offset.


I've tried playing around with TimeLineMax and TweenGroup.allTo() and a few other permutations but I just cant find the right mix.


I'd appreciate any suggestions.

Link to comment
Share on other sites

I need a way to sequence the first two moves for each creature but wrap that in an overall movement with an offset.


What does that mean? It should be pretty easy to set up a sequence with TimelineLite or TimelineMax but I don't understand what you're referring to with the "offset". Have you watched the video at http://www.greensock.com/timeline-basics/ yet? If not, I think it might be helpful.

Link to comment
Share on other sites

I think the 'offset' is a delay to make each creature start its movement at separate times.


The problem is the logic. The creatures can (and maybe should) be created at once, but they start with an idle state, waiting to proceed, you can even use a tween to count the time but maybe that's a bit overboard. You can either set a timer (not that good idea) or check how much time has passed between frames, add that time to an internal counter and when enough time has passed, set the state of the creature to moving and create the first tween to the first location. After that, proceed as you are doing. If you do it like this, you can even have pathfinding with multiple paths.


Timeline could help but it would take the flexibility of your game code.


Is your multiplayer hotseat or server-side? This also matters.

Link to comment
Share on other sites

Vlad has the right Idea.


Think of creature movement in a basic tower defence game.





Creature1.x - tweened to waypoint.x

Creature1.y - tweened to waypoint.y //These Two should be sequenced. first X, once complete, then Y


but a few seconds later:


Creature2.x - tweened to waypoint.x

Creature2.y - tweened to waypoint.y //These Two should be sequenced. first X then Y






So i need to group those two (x then y) tweens one after another as if its one tween,


then add a series of these to objects that start moving a few seconds after each other.


I've seen the Tutorial and Perused the Docs and It not a problem with understanding the code(I think) Its more of a logic issue.

Right, I will put the code up tommorrow when im at my own computer again.

Link to comment
Share on other sites

Well, eh, heres my code anyway:




	public function march(){

		//cycle through creatures
		for(var m:int = 0;m
			//Cycle through targets
			for(var t:int = 0; t

			}//internal for

		}//external for



So the objects move one after another. Is there anything i can put in the external for loop to add , lets say, this timeline to a super-timeline over and over and tween the resulting timeline with each group starting a few seconds after the last

Link to comment
Share on other sites

It's just my opinion, but you shouldn't even have a loop appending moves in the first place. Here's how I'd handle that.


On your level controller class, an update method was called every frame:


public function update():void {

// Update clock counters
var currentTime:int = getTimer();
countdown += currentTime - lastTime;
lastTime = currentTime;

// If one second (1000ms) has passed, create a new creep and reset the countdown
if (countdown > 1000 && nOfCreeps < maxCreeps) {
	myCreepsArray.push(new Creep());
	countdown -= 1000;


Your creep should have the following methods:


public function Creep() {
// Just move!

public function nextStep():void {
// Every creep is independent, no need for loops
if(nextX == 0) nextX = x; // This allows to have vertical movement only
if(nextY == 0) nextY = y; // And this allows to have horizontal movement only
myTween:TweenLite = TweenLite.to(this, time, { x:nextX, y:nextY, onComplete:nextStep } );


This way, each creep is independent, you just a need a list of nextX and nextY. Putting a timeline in all the creeps and then controlling the time to make them move is seriously a bad idea for game development, not because of Timeline itself (which can be useful in this situation) but because you want to handle the code in a separate and modular way in order to keep everything in your control.


What I'm saying is: game development wise, you should have the Tween lib classes controlling transitions, not logic.

Link to comment
Share on other sites

Your right.


This is like the time I was learning design patterns, I was so overwhelmed by their versatility I threw them at every problem and only complicated matters.


I Just found TweenMax and am a little overzealous. I'm sure the novelty will wear off soon and I will start using it respectfully :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.