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

  • SEARCH
  • Home
  • 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 7858895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:39:42+00:00 2026-06-02T21:39:42+00:00

I am trying to place an object at my players current position but when

  • 0

I am trying to place an object at my players current position but when i move away the object sticks to my player. I kind of know why it sticking to my player but i cant think of any other code to use.

Hero is my player that i move around the screen.

Thanks Lochy

var trap1:trap = new trap();

function keydown(event:KeyboardEvent) :void {


    if(event.keyCode ==32)
    addChild(trap1);
    trap1.x = hero.x;
    trap1.y = hero.y;
  • 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-06-02T21:39:43+00:00Added an answer on June 2, 2026 at 9:39 pm

    There are several ways to accomplish this task. There is basically a detail you have to know. In Flash, the display list is responsible for managing elements on the screen. The DisplayObject and DisplayObjectContainer classes provide the API to access and manipulate the display list.

    A naive approach would be

    function placeAbove(d1:DisplayObject, d2:DisplayObject):void
    {
      if (!d1 || !d2) return;
    
      d1.x = d2.x;
      d1.y = d2.y;
    }
    

    But when both DisplayObjects have different parents, the DisplayObjects are not part of te same coordinate system, so this little method won’t work. I coded a small example:

    package
    {

      import flash.display.DisplayObject;
      import flash.display.DisplayObjectContainer;
      import flash.display.Sprite;
      import flash.geom.Point;
    
      public class Points extends Sprite
      {
        public function Points()
        {
          const child1:Sprite = createSquare(50, 50, 0xFF0000, .5)
              , child2:Sprite = createSquare(100, 100, 0x0000FF, .5)
              , container1:Sprite = new Sprite()
              , container2:Sprite = new Sprite();
    
          DisplayObjectContainer(placeRandomly(addChild(container1))).addChild(child1);
          DisplayObjectContainer(placeRandomly(addChild(container2))).addChild(child2);
    
          placeAbove(child1, child2);
        }
    
        private function createSquare(width:Number, height:Number, color:uint = 0, alpha:Number = 1):Sprite
        {
          const sprite:Sprite = new Sprite();
          sprite.graphics.beginFill(color, alpha);
          sprite.graphics.drawRect(0, 0, width, height);
          sprite.graphics.endFill();
    
          return sprite;
        }
    
        private function placeAbove(child1:Sprite, child2:Sprite):void
        {
          const point:Point = child2.localToGlobal(new Point(0, 0))
                  , point2:Point = child1.globalToLocal(point);
    
          child1.x = point2.x;
          child1.y = point2.y;
        }
    
        private function placeRandomly(displayObject:DisplayObject):DisplayObject
        {
          displayObject.x = Math.random() * 100;
          displayObject.y = Math.random() * 100;
    
          return displayObject;
        }
      }
    }
    

    The Application generate 2 squares and adds it into different parents. The containers (parents) are placed randomly on the screen and so are the two child display objects. The placeAbove method does all the magic. It calculates the position from the second display object globally and the maps it to the first display objects target local position within the parent’s coordinate system.

    Ii hope it helps.

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

Sidebar

Related Questions

I'm trying to place an object to my cursor location, but there is a
I am trying to place a value (from an object) in a variable and
I am trying to place a Loading Animation onto my page but have never
I am trying to rotate a canvas with canvas.rotate and move an object on
I'm having trouble with trying to use make to place object files in a
I am trying to place API variables on ONE class file or mxml file
I'm trying to place a nice border around an image that's 250x250, using only
I was trying to place some simple effects on an image using HBox using
I'm trying to place a branding bar (320x22) above the UINavigationBar in a UINavigationController.
I'm trying to place a modal dialog within a form to aid in filling

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.