Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
ActionScript 3 HitTest Problem
Old 10-21-2007, 04:51 PM ActionScript 3 HitTest Problem
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
I have made a simple game using AS3, mainly using this class;

Code:
package {
    import flash.text.TextField;
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.ui.Keyboard;
    public class Avoider extends MovieClip{
        public var target:MovieClip = new MovieClip;
        public var player:MovieClip = new MovieClip;
        public var unicounter:int = 0;
        public function Avoider(){
            init();
        }
        private function init() {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
            drawPlayer();
            drawNewTarget();
            drawEnemy();        
        }
        public function drawNewTarget(){
            var thex:uint= randRange(30,520);
            var they:uint= randRange(30,370);
            target.graphics.beginFill(0x0000FF);
            target.graphics.drawRect(0, 0, 20, 20);
            target.x = thex;
            target.y = they;
            target.name = "Target";
            target.graphics.endFill();
            addChild(target);
            target.addEventListener(Event.ENTER_FRAME,gotToTarget);
        }
        public function drawPlayer(){
            player.graphics.beginFill(0x000000);
            player.graphics.drawCircle(10, 10, 10);
            player.x = 200;
            player.y = 200;
            player.name = "Player";
            player.graphics.endFill();
            addChild(player);
        }
        public function drawEnemy(){
            var thex:uint = randRange(50,500);
            var they:uint = randRange(50,350);
        
            var anenemy = new Enemy;
            anenemy.graphics.beginFill(0xFFFFFF);
            anenemy.graphics.lineStyle(2,0x000000);
            anenemy.graphics.drawCircle(10, 10, 10);
            anenemy.x = thex;
            anenemy.y = they;
            anenemy.name = "Enemy";
            anenemy.graphics.endFill();
            addChild(anenemy);
            anenemy.addEventListener(Event.ENTER_FRAME,touchPlayer);
            if(randRange(0,100) > 50){
            anenemy.addEventListener(Event.ENTER_FRAME,moveVertical);
            } else{
            anenemy.addEventListener(Event.ENTER_FRAME,moveHorizontal);
            }
            var something:MovieClip = new MovieClip;
            something.graphics.lineStyle(3)
            something.graphics.drawCircle(0,0,3);
            something.x = thex;
            something.y = they;
            addChild(something);
        }
        private function randRange(start:Number,end:Number):Number {
            return Math.floor(start + Math.random() * end - start);
        }
        private function moveVertical(e:Event){
            if(e.target.mydirection == 0){
                e.target.y += 4;
            } else if(e.target.mydirection == 1){
                e.target.y -=4;
            }
            if(e.target.hitTestObject(wallbottom)){
                e.target.mydirection = 1;
            }
            if(e.target.hitTestObject(walltop)){
                e.target.mydirection = 0;
            }
            
        }
        private function moveHorizontal(e:Event){
            if(e.target.mydirection == 0){
                e.target.x += 4;
            } else if(e.target.mydirection == 1){
                e.target.x -=4;
            }
            if(e.target.hitTestObject(wallright)){
                e.target.mydirection = 1;
            }
            if(e.target.hitTestObject(wallleft)){
                e.target.mydirection = 0;
                }
        }
        private function touchPlayer(e:Event){
            if(e.target.hitTestObject(player) && !e.target.hastouched){
                e.target.hastouched = true;
                for(var i = (numChildren - 1); i > 5; i--){
                    removeChildAt(i);
                }
            e.target.removeEventListener(Event.ENTER_FRAME,touchPlayer);
            
            scoretext.text = "Score: 0";
            
            for(var i = 0; i < numChildren; i++){
                trace(getChildAt(i).name);
            }
            drawNewTarget();
            drawEnemy();
            }
            
        }
        private function gotToTarget(e:Event){
            if(e.target.hitTestObject(player)){
            e.target.x = randRange(30,520);
            e.target.y = randRange(30,370);
            drawEnemy();
            unicounter++;
            scoretext.text = "Score: " + String((unicounter * 10))
        }
            
        }
        public function onKeyboardDown(event:KeyboardEvent):void {
            stage.focus = this;
            if (event.keyCode == Keyboard.UP) {
                if (!player.hitTestObject(walltop)) {
                    player.y -= 5;
                }
            }
            if (event.keyCode == Keyboard.DOWN) {
                if (!player.hitTestObject(wallbottom)) {
                    player.y += 5;
                }
            }
            if (event.keyCode == Keyboard.LEFT) {
                if (!player.hitTestObject(wallleft)) {
                    player.x -= 5;
                }
            }
            if (event.keyCode == Keyboard.RIGHT) {
                if (!player.hitTestObject(wallright)) {
                    player.x += 5;
                }
            }
        }
    }
}

However, once the player has touched an enemy, the game registers a collision between player and enemy even when these is none, at seemingly random intervals. Any ideas?

Thanks,
Harry
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
 
Register now for full access!
Reply     « Reply to ActionScript 3 HitTest Problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14902 seconds with 12 queries