A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Simple ShooterGame Duck Hunt

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    1

    Simple ShooterGame Duck Hunt

    so, im new to flash, in highschool. 17. im trying to basically make a replica of the old nintendo game Duck Hunt, but amost a versioin of my own, diffrent symbols, along with a gun at the bottom with rotation. Im having troubles getting the ducks to be removed when they are clicked on. i know there is some problem with my code that i cannot see. Ive looked at it blankly for 3 days trying to debug it. ive basically made a mess of it.
    This is my Documents class, name DuckHunt.as
    package
    {
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.ui.Mouse;
    import flash.utils.*;
    import flash.events.MouseEvent;

    public class DuckHunt extends MovieClip
    {
    public var flock:Array;
    public var enemyuck;
    public var CrossHair:crossHair;
    public var enemyTimer:Timer
    public var myTimer:Timer;
    Mouse.hide();
    public var duckXSpeed:Number //X Speed of the duck
    public var duckYSpeed:Number //Y Speed of the duck
    public var speed:Number;
    public var newDuckuck;
    public var secondX:Number;
    public var secondY:Number;


    public function DuckHunt()
    {
    Mouse.hide();
    flock = new Array();
    var newDuck = new Duck(100, 313);
    flock.push( newDuck );
    addChild( newDuck );

    secondX = 20;
    secondY = 20;


    CrossHair = new crossHair();
    addChild( CrossHair );
    CrossHair.x = mouseX;
    CrossHair.y = mouseY;

    trace('MyTimer');
    myTimer = new Timer ( 1 );
    myTimer.addEventListener( TimerEvent.TIMER,moveThings);
    myTimer.start();

    trace('enemyTimer');
    enemyTimer = new Timer(500);
    trace ('Event Listener');
    enemyTimer.addEventListener(TimerEvent.TIMER, onTick );
    enemyTimer.start();


    }
    public function onTick( timerEvent:TimerEvent ):void
    {
    if ( Math.random() < 0.2 )
    {
    var randomX:Number = Math.random() ;
    var newDuckuck = new Duck(randomX, 313);
    flock.push( newDuck );
    addChild( newDuck );
    newDuck.addEventListener(MouseEvent.CLICK, die);
    newDuck.addEventListener(MouseEvent.CLICK, remove);

    }
    }
    public function die(evt:MouseEvent):void
    {
    trace('Die');

    var Death = new Die;
    addChild(Death);
    Death.x=CrossHair.x;
    Death.y=CrossHair.y;
    if(MouseEvent.CLICK)
    {
    trace('die');
    speedUp()
    this.removeChild(flock);

    }
    }
    public function moveThings( timerEvent:TimerEvent ):void
    {
    Mouse.hide();
    CrossHair.x = mouseX;
    CrossHair.y = mouseY;
    for each ( var enemyuck in flock )
    {
    enemy.moveABit();
    if (enemy.y<=-18)
    {
    enemy.bounceEnemy()
    }
    if (enemy.x>=568)
    {
    enemy.bounceEnemy()
    }
    }
    }
    public function remove(evt:MouseEvent):void
    {

    speedUp()

    }
    public function addBackGround()
    {
    var backGround = new BackGround;
    backGround.x = 0;
    backGround.y = 0;
    trace ('add background');
    addChild(backGround);
    }
    public function speedUp()
    {
    x = x + secondX;
    y = y - secondY;
    }
    }
    }
    and this is my Enemy Class, called Duck.as
    package
    {
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.Event;
    import flash.display.Stage;
    import flash.ui.Mouse;
    import flash.utils.*;


    public class Duck extends MovieClip
    {
    public var enemyuck;
    public var xSpeed:Number; //X Speed of the duck
    public var ySpeed:Number ; //Y Speed of the duck
    public var secondX:Number;
    public var secondY:Number;
    public var speed:Number;


    public function Duck (startX:Number, startY:Number)
    {
    x = startX = Math.random()*568;
    y = startY = Math.random()*300+58;
    xSpeed = Math.random()*2;
    ySpeed = Math.random()*2;

    secondX = Math.random()*4;
    secondY = Math.random()*4;

    }

    public function moveABit():void
    {
    x = x + xSpeed;
    y = y - ySpeed;
    }
    public function bounceEnemy():void
    {
    y = y + (ySpeed*1000);
    x = x - (xSpeed*1000);

    }
    public function remove():void
    {
    trace('remove');
    removeChild(enemy);
    }


    }
    }
    my .fla file is pretty basic, a BackGround symbol, exported for as, a Duck symbol, exported for as and a crossHair symbol exported for as. on the stage i have my background already there.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Use [code] tags rather than quote tags for posting code. It will preserve indentation.

    It's hard to read without indentation like it is now. But I see you've got some very wrong stuff.

    What errors are you getting?

    You've got an enemy variable in Duck. But Duck IS the enemy. You never intialize enemy either. You should remove that. Which means you can't call removeChild(enemy), which is okay because you never put enemy on the display. Because it never existed. In remove, you would need to remove the Duck from its parent. But you should probably not do that from within Duck. Do that from the document class.

    I also see that you have removeChild(flock), but flock is not a DisplayObject. It is an Array. You can't do that. You'll have to loop through flock, removing all the instances in it. I don't get why you're trying to remove the whole flock in die anyway. And you don't need to test whether the event is a CLICK, because you only added it to respond to CLICK.

    Why are you calling speedup in remove instead of something that, you know, removes something? Why are you adjusting the root instance's x and y properties in speedup? That doesn't speed anything up. And why would you want to move the root anyway?

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