开发者

getting an area of a object

开发者 https://www.devze.com 2022-12-15 08:48 出处:网络
I am trying to get the four corners of my object. To test out whether its accurate enough. use a method to trace a message when my mouse passes the objects bottom, left, right, or top. when the object

I am trying to get the four corners of my object. To test out whether its accurate enough. use a method to trace a message when my mouse passes the objects bottom, left, right, or top. when the object is positioned at coord 0,0. it works perfect. but when I reposition it. It becomes off and I have no idea why. here is my code.

in my class, there is a loop that has 2 methods.updateArea(). and checkball. update area constant give update to where the object is. my display object forcus point is at the top left on my Flash Stage. so I use the full width and height. again. it works fine at coordinate 0,0 and when I move it, it doesnt.

package com.objects {

    import flash.display.Spr开发者_JAVA技巧ite;
    import flash.events.*;
    import flash.display.Stage;
    import flash.geom.Rectangle;

    public class Brick extends Sprite {

        public var Points = 100;
        public var bWidth:Number = 50;
        public var bHeight:Number = 20;
        private var left:Number;
        private var right:Number;
        private var top:Number;
        private var bottom:Number;
        public var ball:Ball;
        private var lastDistance:Number;

        public var hit:Boolean;


        public function Brick():void
        {
            addEventListener(Event.ENTER_FRAME, loop);
        }

        private function loop(e:Event):void
        {
            updateArea();
            checkBall();
        }
        private function updateArea():void {
            left = x;
            right = x + bWidth;
            top = y;
            bottom = y + bHeight;
        }

        private function checkBall():void
        {
            if(mouseY < bottom)
            {
                trace("Works!");
            }
        }
        public function getBall(ball:Ball):void
        {
            this.ball = ball;
        }

        public function xDir():Number
        {
            if ((ball.xDir * ball.xspeed) < 0)
            {
                return(-1);
            }
            else if ((ball.xDir * ball.xspeed) > 0)
            {
                return (1);
            }           

            return 0;
        }

        public function yDir():Number
        {
            if (ball.yspeed < 0)
            {
                return(-1);
            }
            else if (ball.yspeed > 0)
            {
                return (-1);
            }           

            return 0;
        }

    }
}


x/y/mouseX/mouseY are always relative to the parent. You can use localToGlobal and globalToLocal to translate from parent to global coordinates.

Also if your object isn't square or has holes, you can use hitTest to test for collision instead of calculating based on coordinates.

0

精彩评论

暂无评论...
验证码 换一张
取 消