2

Pretty new flixel/AS3 user here, though not entirely new to coding. Anyways, the error is pretty straightforward in it's cause, but not it's solution.

"Error: Access of possibly undefined property through a reference with static type org.flixel:FlxSprite."

It gets thrown 6 times, twice with the property scrap.selected and four times with scrap.distanceToMouse - I've marked each line that throws an error with comments.

I've searched throughout the web and StackOverflow specifically. It looks like a pretty common problem, but I haven't been able to apply any solutions to my specific situation. Anyways, Here's the relevant code...

ScrapManager.as

    if (FlxG.mouse.pressed)
    {
        var ClosestDistance:int = 500; 
        for each (var scrap:FlxSprite in this)
        {
            scrap.selected = false; //error here!~ 
            var dx:int = scrap.x - FlxG.mouse.screenX;
            var dy:int = scrap.y - FlxG.mouse.screenY;
            scrap.distanceToMouse = (dx * dx) + (dy * dy); //error here!~

            if (scrap.distanceToMouse < ClosestDistance) //error here!~
            {
                ClosestDistance = scrap.distanceToMouse; //error here!~
            }
        }
        for each (var scrap:FlxSprite in this) 
        {
            if (scrap.distanceToMouse == ClosestDistance) //error here!~
            {
                scrap.selected = true; //error here!~
            }
        }
    }

Scrap.as

package 
{
    //import stuff...

    public class Scrap extends FlxExtendedSprite
        {
            public var selected:Boolean = false; 
            public var distanceToMouse:int; 
            //and more stuff...

I think (hope) that's all that's needed to solve this, but I'll be happy to provide more if needed. Thanks for reading. :D

2 Answers2

1

My guess is either (a) you changed the FlxSprite to static, or (b) your Scrap class is static.

Mar
  • 7,765
  • 9
  • 48
  • 82
  • Unfortunately, "Static" is not so much as mentioned in Scrap, ScrapManager, FlxSprite, or FlxExtendedSprite. Thanks anyways, though. – Kris with a K Jan 31 '12 at 23:31
1
for each (var scrap:FlxSprite in this)

Are you certain that every property in "this" is an instance of FlxSprite? Maybe you need to store a collection of the FlxSprites inside ScrapManager and loop through those instead?

I'd suggest tracing out "scrap" in those loops to make sure it is the right data type. If it is, and it is still giving out those errors, you should be able to narrow it down to the specific object that's giving you the problem.

James Tomasino
  • 3,520
  • 1
  • 20
  • 38
  • Oh fu- I totally forgot, but I changed Scrap to be a FlxExtendedSprite. I imagine that would probably mess things up huh? Can't get to my code right now but hopefully that'll fix it. Thanks for the tip, can't believe I forgot that >. – Kris with a K Jan 31 '12 at 15:05
  • Be sure to mark the correct answer when you're done. Welcome to Stack Overflow! – Mar Jan 31 '12 at 18:23
  • AHAH! Your answer was even closer than I thought. I had this line too in ScrapManager: `var scrap:Scrap = Scrap(getFirstAvailable());` I guess that means I couldn't access the variables inside Scrap because I was looking at FlxSprite (where they didn't exist), right? Thank you very much! It's not working quite yet but that's because of my math, it's all error free! – Kris with a K Jan 31 '12 at 23:53
  • Oh and one last mistake, for anyone who comes after; I wrote `if (FlxG.mouse.pressed)` when I should have written `if (FlxG.mouse.pressed())` – Kris with a K Feb 01 '12 at 00:24
  • 1
    He James, sorry if I'm spamming you, but I want to make my solution clear to anyone else who checks this out. Turns out in addition to all the above, the code has also got to read `this.members`, not just `this`. Which in hindsight is pretty obvious. And now I can say it is _finally_ working. Again, thanks to everyone for all the help! – Kris with a K Feb 01 '12 at 01:19