HaxeFlixel / flixel-docs

Documentation for HaxeFlixel
72 stars 135 forks source link

Tutorial Chapter 12: Enemy has no variable "_halfWidth" #188

Closed KosnIre closed 7 years ago

KosnIre commented 7 years ago

Description

In the Tutorial Chapter 12, step 8: the supplied code uses a variable called _halfWidth

we set the position of our sound to wherever our enemy is (to the bottom of his sprite - where his feet are), and then play the sound.

if ((velocity.x != 0 || velocity.y != 0) && touching == FlxObject.NONE)
 {
     _sndStep.setPosition(x + _halfWidth, y + height);
     _sndStep.play();
 }

Using this code results in the following error: source/Enemy.hx:70: characters 28-38 : Unknown identifier : _halfWidth

Analysis

I'm not sure what was intended here, but there are a few options to correct is:

A. FlxSprite has a variable called _halfSize, which is set in resetFrameSize(). I'm not sure if this is the intended use though.

B. In my Enemy class, I declared a Float called _halfWidth and set it in the constructor function: _halfWidth = width * 0.5;

C. In the demo source code, this is calculated each frame when making the call to setPositon: _sndStep.setPosition(x + frameWidth / 2, y + height);

I would like someone to weigh in on the "best approach" here.

Gama11 commented 7 years ago

Probably A was intended, but _halfWidth is internal and shouldn't be used like that (and has in fact been changed to _halfSize at some point it looks like). C seems like a good fix. 👍