Skip to main content
Participant
September 29, 2011
Question

Student Learning-Hittesting

  • September 29, 2011
  • 1 reply
  • 575 views

Hi everybody!

I'd Just like to say thankyou to everybody for taking a look at my post even if you dont reply! and Double thankyou to those that do reply!

Im working in classes something Ive never done before!

Currently the class creates a small bird object that moves in both X and y and when the character hits it its destroyed, makes the character jump and doubles the score.(plus some other junk!)

What I want is: when my character hits this object I want him to land on it like a platform and stay on it while it moves! Im changing it from a bird to a cloud!

I have posted the full class below but I know( i think i do) that the only really important part I need to change is this:

if(this.hitTestObject(_root.mcMain)){//if this touches the main character

  _root.mainJumping = true;//make him jump

  _root.jumpSpeed = _root.jumpSpeedLimit*-1;//reset the jumpSpeed

  var scoreText:ScoreAdd = new ScoreAdd();

  _root.bellHolder.addChild(scoreText);//add some text to the stage

  scoreText.x = this.x;//set the coordinates for the text

  scoreText.y = this.y;

  scoreText.txtScore.text = 'Double Score';//show that the score was doubled

  this.removeEventListener(Event.ENTER_FRAME, eFrame);//remove the listeners

  _root.bellHolder.removeChild(this);//and finally remove him from the stage

  _root.score *= 2;

  _root.startedJumping = true;

  }

What i think i need to achieve is:

Trying to narrow the hitest to the top of the platform

stop the player from falling any further

then set the players X and Y to follow that of the platform object untill I jump off it

so far all I have is:

if (_root.mcMain.hitTest(this._x, this._y-11, true) or _root.mcMain.hitTest(this._x+5, this._y-11, true) or _root.mcMain.hitTest(this._x-5, this._y-11, true)) {

  _root.jumpSpeed = 0;

  _root.mcMain._x += this._x;

  _root.mcMain._y += this._y;

}

Can someone please help me figure this out! OR explain to me what I need to achieve the effect I want>?

I can post more of the code if that helps! I will be checking this over the next 48hrs!

Thanks again for taking a look I know its a long shot but I have nowhere else to ask!

cheers

Jackson

-----------------FULL CLASS CODE----------------------------------------------------------------------------------------------------------------------------------

package{

          import flash.display.MovieClip;

          import flash.events.*;

          public class Bird extends MovieClip {

 

                    var _root:Object;//this will symbolize the main timeline

                    var speed:int = 5;//how fast this bird will move side to side

 

                    public function Bird() {

                              addEventListener(Event.ADDED, beginClass);

                              addEventListener(Event.ENTER_FRAME, eFrame);

                    }

 

                    private function beginClass(e:Event):void{

                              _root = MovieClip(root);

 

                              this.x = -50;//setting x value off the stage

                              this.y = _root.bellTop;//set the y value off the stage

                    }

                    private function eFrame(e:Event):void{

                              this.y += 3;//move the bell slowly downwards

 

                              this.x += speed;//moving this guy based on it's speed

                              if(this.x >= 537.5){//if it goes too far right

                                        speed = -5;//make it go backwards

                                        scaleX = -1;//and turn it around

                              }

                              if(this.x <= 12.5){//same with too far left

                                        speed = 5;

                                        scaleX = 1;

                              }

 

 

                              if(this.hitTestObject(_root.mcMain)){//if this touches the main character

                                        _root.mainJumping = true;//make him jump

                                        _root.jumpSpeed = _root.jumpSpeedLimit*-1;//reset the jumpSpeed

                                        var scoreText:ScoreAdd = new ScoreAdd();

                                        _root.bellHolder.addChild(scoreText);//add some text to the stage

                                        scoreText.x = this.x;//set the coordinates for the text

                                        scoreText.y = this.y;

                                        scoreText.txtScore.text = 'Double Score';//show that the score was doubled

                                        this.removeEventListener(Event.ENTER_FRAME, eFrame);//remove the listeners

                                        _root.bellHolder.removeChild(this);//and finally remove him from the stage

 

                                        _root.score *= 2;

 

                                        _root.startedJumping = true;

 

                              }

 

                              if(_root.gameOver){//if the game is over

                                        this.removeEventListener(Event.ENTER_FRAME, eFrame);//remove the listeners

                                        _root.bellHolder.removeChild(this);//and remove from stage

                              }

                    }

          }

}

This topic has been closed for replies.

1 reply

Inspiring
September 29, 2011

To achieve this effect, you would have to make the players x and y the same as the birds on the hit.

Something like this:

(PS i have not read your code, its not formatted in a way easy to read, you can email itto me if you like for me to do a proper analysis I will PM you my email)

but anyways

if(playerInstance.hitTestObject(birdInstance))

{

     player.x = bird.x;

     player.y = bird.y;

}

this is not the most effective or accurate, but like i said i have not read your code. A bitmap hit test will be more accurate cycling the birds through a array to decide which one is hit aswell.

Participant
September 29, 2011

Hi there thanks for the reply

Here is all my game code. In the attachment.

I have changed the bird graphic to a cloud graphic.

but the .as class file is still called bird.

Thanks for your time!

Date: Thu, 29 Sep 2011 12:42:05 -0600

From: forums@adobe.com

To: haxjack@live.com.au

Subject: Student Learning-Hittesting

Re: Student Learning-Hittesting

created by smitchell94 in Action Script 3 - View the full discussion

To achieve this effect, you would have to make the players x and y the same as the birds on the hit. Something like this: (PS i have not read your code, its not formatted in a way easy to read, you can email itto me if you like for me to do a proper analysis I will PM you my email) but anyways if(playerInstance.hitTestObject(birdInstance)){ player.x = bird.x; player.y = bird.y;} this is not the most effective or accurate, but like i said i have not read your code. A bitmap hit test will be more accurate cycling the birds through a array to decide which one is hit aswell.

Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page:

To unsubscribe from this thread, please visit the message page at . In the Actions box on the right, click the Stop Email Notifications link.

Start a new discussion in Action Script 3 by email or at Adobe Forums

For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.

Inspiring
September 29, 2011

Hi, as far as i can see, you did not attach the files. Or im just being blind. Can you email it to me at s.mitchell94@hotmail.co.uk.