Author Topic: Errors trying to get angle from player to mouse  (Read 2065 times)

timbowman1

  • New Member
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
I'm trying to get the right angle from the player to the mouse for bullets.
I'm trying to calculate the angle like this:
Code: [Select]

private function setAimAngle():void
{
this.aimAngle = FlxU.getAngle(new FlxPoint(player.x, player.y), new FlxPoint(FlxG.mouse.x, FlxG.mouse.y))
}

The program runs and then I get the following error:
[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.

I set the variable aimAngle above like so: public var aimAngle:Number = 0;

Can anybody help me?

Dmeville

  • Member
  • **
  • Posts: 67
  • Karma: +0/-0
    • View Profile
Re: Errors trying to get angle from player to mouse
« Reply #1 on: Wed, Dec 14, 2011 »
Doesn't that mean something hasn't been created yet?

Try tracing out the player and see if it returns null or not. 
Code: [Select]
trace("Player: ", player);
If it does trace null, that means that the player hasn't been instantiated yet, and you are trying to use properties (x/y) from something that doesn't exist! 

timbowman1

  • New Member
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Errors trying to get angle from player to mouse
« Reply #2 on: Wed, Dec 14, 2011 »
Haha, wow I am a NOOB! That IS what was happening.. I was originally going to code the gun in the Player class but things got confusing and it started working once I coded it into the PlayState. I found a function on photonstorm's power tools that fires at the mouse, so I just used that instead.

Thanks!

p.s. photonstorm if you read this, i love you.

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: Errors trying to get angle from player to mouse
« Reply #3 on: Thu, Dec 15, 2011 »
:)
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

c023-DeV

  • Game Artisan
  • Active Member
  • ***
  • Posts: 176
  • Karma: +0/-0
    • View Profile
    • DeV-ZoO
Re: Errors trying to get angle from player to mouse
« Reply #4 on: Sat, Dec 17, 2011 »
Just on further thought:

In Photons Powertools there is:

FlxVelocity.angleBetweenMouse(this, true);

and my new fav:

FlxVelocity.moveTowardsMouse(this, speed,0)

=D
If you aint got no dedication, you won't get no education!