A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: help with my enemy ai code

  1. #1
    Slacker Extraordinaire
    Join Date
    Feb 2002
    Posts
    23
    I'm working on the code for the enemies of my game

    I just can't get this code to work

    It's fairly simple:
    Find out hero's x location as xspot
    move to xspot - when enemy x = xspot
    get next xspot

    here's the code
    Code:
    onClipEvent (load) {
    // find where hero is
    xspot = _root.hero._x;
    }
    
    onClipEvent (enterFrame) {
    if(this._x < xspot) {  // move right
    	this._x += 2;
    }
    if(this._x > xspot) { // move left
    	this._x -= 2;
    }
    if(this._x == xspot) { // arrived at xspot - get new xspot
    	xspot = _root.hero._x;
    }
    }

    This is just the first part, but I just can't
    figure it out. Once the MC gets to xspot it stays there.
    It's not going to the next xspot.
    I did a trace on xspot and the value never changes.

    Anyone know what I'm doing wrong?

    Thanks

  2. #2
    SaphuA mosterdfles_flash's Avatar
    Join Date
    Jan 2002
    Location
    Tha Couch
    Posts
    935

    :)

    the onload event only works when you load the enemy MC ...

    So it only track's the hero's position once and then he doesn't anymore... unless you change it into the enterFrame even't...

    Code:
    onClipEvent (enterFrame) {
    xspot = _root.hero._x;
    
    if(this._x < xspot) {  // move right
    	this._x += 2;
    }
    if(this._x > xspot) { // move left
    	this._x -= 2;
    }
    if(this._x == xspot) { // arrived at xspot - get new xspot
    	xspot = _root.hero._x;
    }
    }

  3. #3
    Slacker Extraordinaire
    Join Date
    Feb 2002
    Posts
    23
    Yeah, but i don't want to track the hero's movement

    I want to get the x position - store it and move the MC to it
    when the MC gets to xspot - THEN it picks a NEW xspot

    the problem with the code seems to be with this line

    Code:
    if(this._x == xspot) { // arrived at xspot - get new xspot
    	xspot = _root.hero._x;
    }
    I want the enemy to be kind of dumb
    this code should change the value of xspot when
    the mc gets to xspot position

  4. #4
    Zakarus's Tale Developer Zakarus2001's Avatar
    Join Date
    May 2001
    Location
    Here, lurking
    Posts
    237
    lazypays is right the reason the value never changes is because it only checks it only gets the xpot once, whent he mc loads. You would need something like this...
    Code:
    onClipEvent(load) {
        xspot = _root.hero._x;
    }
    onClipEvent(enterFrame) { 
       if(this._x < xspot) {  // move right
          this._x += 2;
       }
       if(this._x > xspot) { // move left
          this._x -= 2;
       }
       if(this._x == xspot) { // arrived at xspot - get new xspot
          xspot = _root.hero._x;
       }
    }
    yeah that should work...
    Have fun
    ~Zakarus2001

    //////////////ADDITION//////////////////
    I think another problem would be that if the xspot is something like 2.002 or something, the enemie will never get tot hat spot, because it only moves in integers of 2.. so maybe you should do something like...
    Code:
    if (this._x >= xspot-1 && this._x <= xspot+1) {
       xspot  = _root.hero._x;
    }
    that should fix it


  5. #5
    Slacker Extraordinaire
    Join Date
    Feb 2002
    Posts
    23
    Yeah, that worked perfectly

    Thanks

    joe

  6. #6
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    yeh
    xspot = _root.hero._x;
    needs to be under onClipEvent(enterFrame) {
    because it needs to be refreshed every frame



  7. #7
    When you know are. Son of Bryce's Avatar
    Join Date
    Aug 2002
    Location
    Los Angeles
    Posts
    838
    How would you set the AI to activate if you are within a certain range of the enemy? Like 100 pixels or something.

  8. #8
    Slacker Extraordinaire
    Join Date
    Feb 2002
    Posts
    23
    Well the AI was for a shooting game I'm making and
    I'm still working on it.

    My idea was to get the x and y position of the hero MC
    -Then find out which distance was FARTHER Away,
    and then move the enemy MC to that specific position
    (either x or y, but not both)

    When the enemy MC reaches that specific X or Y
    position, it picks a new position based on hero MC.

    Since the enemy MC is shooting at you, he only has to line
    up on one axis to get a clean shot. By moving him to
    the farther x or y position he doesn't appear to move at
    an angle straight at you.

    does that make sense?

    anyway here's a quick example of what I was working on
    it's not working exactly right, but it's getting close
    -use the arrow keys to move and space bar to shoot


    http://www.lazypays.com/flash/sample_ai.html

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