Tower Defence

Non-platform specific questions.

Tower Defence

Postby JimJamJom56 » Sun Mar 04, 2012 4:35 pm

How would I make a tower defence game in GE?
I would quite like to make one and any help would be appreciated! :D
Don't be surprised if I say something Pocket PC related :P

Press any key to continue, or any other key to cancel.
User avatar
JimJamJom56
 
Posts: 36
Joined: Fri Jun 24, 2011 3:48 pm
Location: Behind You
Score: 2 Give a positive score

Re: Tower Defence

Postby skydereign » Mon Mar 05, 2012 7:56 am

You create actors with animations to represent the desired objects you need in your game, and give them code to execute what they need to do. It's the same with every other game, the trick is to know what you want them to do. I believe you are fully capable of doing that much. Now if you have problems implementing or even conceptualizing how you would create a certain type of object from a tower defense game, I'll be happy to help, but asking a question that large won't net a very helpful response. It also depends on the type of tower defense game.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Tower Defence

Postby JimJamJom56 » Mon Mar 05, 2012 6:37 pm

I am looking to make a tower defence like this one:
http://www.flasharcade.com/tower-defence-games/play/azgard-tower-defense.html

I'm not sure on how to make a turret target a enemy within a certain range, maybe the closest enemy?
Don't be surprised if I say something Pocket PC related :P

Press any key to continue, or any other key to cancel.
User avatar
JimJamJom56
 
Posts: 36
Joined: Fri Jun 24, 2011 3:48 pm
Location: Behind You
Score: 2 Give a positive score

Re: Tower Defence

Postby skydereign » Mon Mar 05, 2012 7:02 pm

To do this it is easiest if you know one of two things. Either how to handle arrays, specifically in gE, or you know about Actor*s. I think using arrays for this is one of the cases where arrays are a better choice (since the cloneindex of every new creep will increase the highest cloneindex). Each time you create a creep, it should find an index in the position array, and record it via an actor variable. In the creep's draw, it should update its position in the array. This way when you have a tower, you can loop through the position array to determine which creep is the closest.
Code: Select all
int i;
for(i=0;i<MAX_CREEPS;i++)
{
    if(creeps[i][INDEX] == -1) // if creep doesn't exist
    {
        creeps[i][INDEX]=cloneindex; // stores the index
        // if you use different actors for each creep you need to record that here as well
        creep_index = i; // sets the actor variable so it knows which creep it is
        break; // escape the forloop
    }
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Tower Defence

Postby JimJamJom56 » Fri Mar 09, 2012 5:21 pm

Sorry, but I don't quite understand how you do that! :? How do I do the position array? And where does the code go?
Don't be surprised if I say something Pocket PC related :P

Press any key to continue, or any other key to cancel.
User avatar
JimJamJom56
 
Posts: 36
Joined: Fri Jun 24, 2011 3:48 pm
Location: Behind You
Score: 2 Give a positive score

Re: Tower Defence

Postby Hblade » Fri Mar 09, 2012 6:18 pm

I'll explain sky's code
Code: Select all
int i;
for(i=0;i<MAX_CREEPS;i++)
{
    if(creeps[i][INDEX] == -1) // if creep doesn't exist
    {
        creeps[i][INDEX]=cloneindex; // stores the index
        // if you use different actors for each creep you need to record that here as well
        creep_index = i; // sets the actor variable so it knows which creep it is
        break; // escape the forloop
    }
}


int i; creates a variable called "i". for(i=0;i<MAX_CREEPS;i++) means, i starts at 0, and if it's less than the max number of creeps, (exmaple: 32), i gets added by 1 up until it reaches the number of creeps, but it does so all in 1 frame. WHILE it's adding the i's up, it's doing code every i++.

The rest he commented already.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Tower Defence

Postby skydereign » Fri Mar 09, 2012 7:43 pm

The idea is, that every creep needs to be stored in the array. If you don't know already, you have to use an index of some sort to access an array, for instance array[10], where 10 is the index. Normally you could use cloneindex for the indexing, except that cloneindex can keep growing (exceeding the bounds of the array). So you need to create an actor variable that holds the creep's position in the array. This variable would need to be set in the actor's create actor event, and loop through an array finding the first empty index.

Assume this is an array of size 6.
[0][1][2][-1][4][-1]
As I mentioned in my previous post, -1 should stand for the creep not existing. So if you were to place a new creep, its create actor event would loop through the array, checking for a -1. When it finds it, it should set the -1 to equal the position in the array, and set its actor variable equal to i as well. If the variable was named creep_index, and we used the above array, it would result in the following.

[0][1][2][3][4][-1]
creep_index=3;

And when a creep dies you need to remove it from the array, using creep_index. Since creep_index is now the actor's key into the array, you just set the array's creep_index position to -1. You can also use the key to set the x and y positions in draw. It's pretty important you understand the idea behind the actual code before you really start to work on the game, otherwise it'll become a mess. That's the main reason why I'm trying not to provide you with all the code.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest