• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Hit Test Object..

Community Beginner ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

Hi ii want my character to walk down the screen and hit the objectWall and then a movieclip then gets added to the stage and plays but it doesnt seem to work and ii dont know why.

import flash.display.MovieClip;

import flash.ui.Keyboard;

import flash.events.Event;

stop();

//1. Set booleans for the status of the arrow presses

var downArrow:Boolean = false;

//2. Add event listeners for the keys

stage.addEventListener(KeyboardEvent.KEY_UP, nokeyHit);

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);

stage.addEventListener(Event.ENTER_FRAME, moveObject);

//3. Function to detect if a key has been hit

function keyHit(event:KeyboardEvent):void

{

          switch (event.keyCode)

          {

                    case Keyboard.DOWN :

                              downArrow = true;

                              break;

          }

}

//4. Function to detect if a key has not been hit

function nokeyHit(event:KeyboardEvent):void

{

          switch (event.keyCode)

          {

                    case Keyboard.DOWN :

                              downArrow = false;

                              break;

          }

}

//5. Function to move the object

function moveObject(event:Event):void

{

          var speed:Number = 2;

          if (downArrow)

          {

                    objectMove.y +=  speed;

          }

          hitWalls();

}

//6. Function to test hitting the walls

function hitWalls():void

{

          var left:Number = 0;

          var right:Number = stage.stageWidth;

          var top:Number = 0;

          var bottom:Number = stage.stageHeight;

          if (objectMove.x  - (objectMove.width / 2) > right)

          {

                    objectMove.x = left - objectMove.width / 2;

          }

          else if (objectMove.x + objectMove.width / 2 < left)

          {

                    objectMove.x = right + objectMove.width / 2;

          }

          if (objectMove.y + (objectMove.height / 2) > bottom)

          {

                    objectMove.y = bottom - (objectMove.height / 2);

          }

          else if (objectMove.y - (objectMove.height / 2) < top)

          {

                    objectMove.y = top + (objectMove.height / 2);

          }

}

objectMove.addEventListener(Event.ENTER_FRAME, testHit);

function testHit(event:Event):void

{

          var Attack:MovieClip = new MovieClip;

 

          if (objectMove.hitTestObject(objectWall))

          {

                    objectMove.removeEventListener(Event.ENTER_FRAME, testHit);

                    addChild(Attack);

          }

}

Any Help Would Be Great Thanks.


TOPICS
ActionScript

Views

684

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 13, 2012 Apr 13, 2012

The problem is in this line...

    var Attack:MovieClip = new MovieClip;

You are just adding a MovieClip object.  It will be an invisible thing because it contains nothing.  If you happen to have some other object that you have assigned a class name, maybe like AttackMC that sits in the library waiting to be added, you would use...

var Attack:AttackMC = new AttackMC();

Nore: It is standard practice to only use Capitalized names for classes, not variables... it just helps someone else understand your

...

Votes

Translate

Translate
LEGEND ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

LATEST

The problem is in this line...

    var Attack:MovieClip = new MovieClip;

You are just adding a MovieClip object.  It will be an invisible thing because it contains nothing.  If you happen to have some other object that you have assigned a class name, maybe like AttackMC that sits in the library waiting to be added, you would use...

var Attack:AttackMC = new AttackMC();

Nore: It is standard practice to only use Capitalized names for classes, not variables... it just helps someone else understand your code easier.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines