A Flash Developer Resource Site

Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30

Thread: Just another tower defense question

  1. #21
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    no, it should always be defined. Look at the code again, the reference to the enemy is outside of any distance checking statement, its inside a for loop. You're referencing your enemies like this:

    in the root, check for a movieclip named "enemy" + the game level + the unqiue enemy id. Suppose game level is 2 and monster_spawned is 3, our enemies need names:

    enemy2_0
    enemy2_1
    enemy2_2

    now, if your game level is undefined or set wrong the reference is going to be broken, hence "undefined". Also, if the unique id has not been set or is counting up wrong then it will be broken as well. And after all that the enemy needs to be named correctly. So:

    * check all names of your enemies.
    * check the level variable
    * check the "u" variable and "monsters_spawned" variable.

    This is really simple stuff.

    if any of those are not what you are expecting then there is a problem.
    lather yourself up with soap - soap arcade

  2. #22
    Member
    Join Date
    Sep 2008
    Posts
    54
    My bad, I just realized what I was saying was wrong this morning, declaration of enemy is not inside dist<=range.

    Ok, but my point here the problem is not in there. Because the code in post #5 also showed the same "undefined" thingy, 1 in every frame; but the tower still rotates as it should be in there.

  3. #23
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    maybe target is defined somewhere else. The rotate code is using two things (as set by you)

    target and attacking.

    if attacking is true and the target is not undefined the tower will rotate. Both variables are declared outside of the "onEnterFrame" function, meaning, they belong to the tower, which also means they can be changed from another function.

    All you need to do here is fix your references. Fix the enemy reference:

    enemy = _root["enemy"+_root.level+"_"+u];

    once you have done that, and that "enemy" variable is not undefined the rest of the code should work correctly as in post #8

    enemy and target should never be undefined
    lather yourself up with soap - soap arcade

  4. #24
    Member
    Join Date
    Sep 2008
    Posts
    54
    Post #8 is never working in the first place. I tried to put trace there, and the queue.length is never > 0, thus 'attacking' is also never true.

    Code:
    if (queue.length > 0) {
    				trace("working");
    				queue.sortOn("priority",Array.NUMERIC);
    				
    				var item = queue[0];
    				
    				attacking = true;
    				target = item.enemy;
    			}
    Could you take a look at my .fla file for a while?

    I have two movie clips there, "scout tower" and "splash tower". Scout tower uses the code you provided, the splash tower uses the code I put in post #3.
    http://rapidshare.com/files/274963973/tower5.fla.html
    I'm sorry for all the troubles. I'm just so desperate, having presentation in 2 hours and still can't figure out the problem here

  5. #25
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    have you been reading what I've said in the last 4 posts? I feel like I'm pulling my hair out. Forget the queue for now, fix the references.

    FIX THIS: _root["enemy"+_root.level+"_"+u];

    plain and simple. I don't have time to look at the fla, i'm busy also.
    Last edited by mr_malee; 09-03-2009 at 12:36 AM.
    lather yourself up with soap - soap arcade

  6. #26
    Member
    Join Date
    Sep 2008
    Posts
    54
    Seriously, what is wrong with that? I don't know which part is wrong from that declaration, thereby I have no idea in how or what to fix.

    I can trace them:
    undefined
    _level0.enemy1_1
    undefined
    _level0.enemy1_1
    _level0.enemy1_2
    undefined
    _level0.enemy1_1
    _level0.enemy1_2
    _level0.enemy1_3
    ...

    Yet so far, my other code works fine with it and it can rotate according to that enemy. Sorry, man, I gotta admit I really don't get what to fix here.

  7. #27
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    I took a quick look, this is annoying me now

    you must create your queue before adding items to it. You didn't copy what I had written down in #8 correctly. I thought you had copied everything so I assumed you had the queue created. You also didn't tell me what the other traces were "trace("found targets " + queue.length);" that would have shown "found targets undefined". Which would have helped me help you.

    Code:
    var target;
    var attacking = false;
    var delay = 12;
    var checkInterval = delay;
    var queue = []; //Here is where the queue is created, its a new array.
    
    this.onEnterFrame = function() {
    also, (although you don't have to) change your loop so you aren't processing undefined items:

    Code:
    for (var u = 1; u <= _root.monster_spawned; u++) {
    undefined is bad, you're wasting cpu cycles processing undefined items.

    The stench of AS2 is very strong now
    Last edited by mr_malee; 09-03-2009 at 01:02 AM.
    lather yourself up with soap - soap arcade

  8. #28
    Member
    Join Date
    Sep 2008
    Posts
    54
    Oh ok, I fogot to put the var queue = [] in that fla.
    I tested things and saved them in different fla, guess I missed that one in the last fla.

    Thanks for pointing out the for loop mistake, I didn't realize that. It seems working now, thanks for getting thorough with me patiently til' now.

  9. #29
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    remember to set the queue length to 0 as well, I noticed you had commented it out.
    lather yourself up with soap - soap arcade

  10. #30
    Member
    Join Date
    Sep 2008
    Posts
    54
    Yeah, I was experimenting with it.
    Many thanks, I owed you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center