Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6143391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:31:09+00:00 2026-05-23T18:31:09+00:00

My code instructs the Enemy that when the Target gets within a certain distance

  • 0

My code instructs the Enemy that when the Target gets within a certain distance that the Enemy fires a bullet at the Target, either left/down(diagonal), down, or right/down(diagonal).
My problem is: the Bullet fires fine but, once the Target is no longer within the set distance – the Bullets curves to down(y+) rather than just continuing on it’s initial trajectory.

private function loop(e:Event) : void
    {

        y += vy;
        x += 0;
        x -= 0;
        if ((target.x - x) > (target.y - y) && (getDistance(target.x, target.y, x, y) < interestDistance / 2))
              x += 5;

        if ((target.y - y) > (target.x - x) && (getDistance(target.x, target.y, x, y) < interestDistance / 2))
              x -= 5;



        if (y > stageRef.stageWidth || y < 0)
        removeSelf();
    }

What can I add to the code to kees the Bullet moving in it’s original trajectory regardless of where the Target has moved to?

Thank you for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:31:09+00:00Added an answer on May 23, 2026 at 6:31 pm

    Your code is a slight mess, think about this for a second:

    When is the trajectory of the bullet defined? Right now, your bullet is acting like a guided missile with broken guidance code (no offense :)).

    Specifially, your bullet is going into y+ because of this line of code:

    y += vy;

    as you can see it is always executed inside the loop even when x+=5 or -5 is not…

    The vy and some vx variable you currently do not have should be initialized at the start of the bullets life, ie probably somewhere in the bullets contructor

    package {
        public class Bullet extends MovieClip{
            public var vx:Number;
            public var vy:Number;
            public var target:MovieClip;
                  public function Bullet(target:MovieClip, startX:Number, startY:Number){
    
                        /*this is a slightly bad idea to let the bullet have any knowledge of
                          the target but for purposes of this explanation lets do it like that.
                          This should better be put where the bullet was created but for now...*/
                         this.target = target;
                         x = startX;
                         y = startY;
                         /*this is slightly better code then your loop and it is only set once
                          instead of all the time*/
                         if(target.x < x-20){
                              vx = -5;
                         }else if(target.x > x+20){
                              vx = +5;
                         } else {vx = 0;}
                         if(target.y < y-20){
                              vy = -5;
                         }else if(target.y > y+20){
                              vy = +5;
                         } else {vy = 0;}
    
                          //now we create the loop itself
                          addEventListener(Event.ENTER_FRAME, loop);
                  }
    
                  private function loop(e:Event) : void{
                       x+=vx;
                       y+=vy;
                       /*this is collision code, bear in mind this should probably also be
                        in an enter frame in the class that created the bullet and the target
                        the bullet simply should not know about the target but alas, long story...*/
                       if(checkCollision(target)){
                          //do something
                       }
                  }
                  /* notice that this function can be used outside of this class as well...
                   this is why i programmed it like this instead of just using the              
                   this.hitTest if*/
                  public function checkCollision(target):Boolean{
                        if(this.hitTest(target)){
                             return true;
                        }
                        return false;
                  }
        }
    }
    

    I hope I did not make any errors in this code…

    Also, it seems to me you only want the bullet to go diagonally to the lower left or lower right… in that case, please modify the constructor so that it simply has something like vy = 5; instead of all this:

    if(target.y < y-20){
         vy = -5;
    }else if(target.y > y+20){
         vy = +5;
    } else {vy = 0;}
    

    I hope this does the trick for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code that is untestable really annoys me. The following things make oo-code untestable: global
(code examples are python) Lets assume we have a list of percentages that add
I am using MVC3 and in certain locations in the code I am using
i am trying to find in this code where it instructs a new browser
I've the following code that uses jQueryMobile Sliders: <!DOCTYPE html> <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta
I'm trying to write code that talks to Google Spreadsheets. We do a bunch
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code: <html xmlns=http://www.w3.org/1999/xhtml> <head> <title>Unusual Array Lengths!</title> <script type=text/javascript> var arrayList = new Array();
Code I have: cell_val = CStr(Nz(fld.value, )) Dim iter As Long For iter =
Code and preview: <html> <head> <title>Testing some CSS</title> <style type=text/css> .dDay { font-size:205% }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.