Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent setting a FlxSprite to an invalid frame #174

Merged
merged 6 commits into from
Sep 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion org/flixel/FlxEmitter.as
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ package org.flixel
{
var sprite:FlxSprite = new FlxSprite();
sprite.loadGraphic(Graphics,true);
totalFrames = sprite.frames;
totalFrames = sprite.numFrames;
sprite.destroy();
}

Expand Down
117 changes: 105 additions & 12 deletions org/flixel/FlxSprite.as
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ package org.flixel
* NOTE: Edit at your own risk!! This is intended to be read-only.
*/
public var frameHeight:uint;
/**
* The total number of frames in this image. WARNING: assumes each row in the sprite sheet is full!
*/
public var frames:uint;
/**
* The actual Flash <code>BitmapData</code> object representing the current display state of the sprite.
*/
Expand Down Expand Up @@ -99,6 +95,15 @@ package org.flixel
* Internal, keeps track of the current index into the tile sheet based on animation or rotation.
*/
protected var _curIndex:uint;
/**
* Internal, tracker for the maximum number of frames that can fit on the tile sheet, used with read-only getter.
* WARNING: assumes each row in the sprite sheet is full!
*/
protected var _maxFrames:uint;
/**
* Internal, tracker for the number of frames on the tile sheet, used with Flash getter/setter.
*/
protected var _numFrames:uint;
/**
* Internal, used to time each frame of animation.
*/
Expand Down Expand Up @@ -188,6 +193,8 @@ package org.flixel
_curAnim = null;
_curFrame = 0;
_curIndex = 0;
_numFrames = 0;
_maxFrames = 0;
_frameTimer = 0;

_matrix = new Matrix();
Expand Down Expand Up @@ -402,9 +409,14 @@ package org.flixel
framePixels = new BitmapData(width,height);
origin.make(frameWidth*0.5,frameHeight*0.5);
framePixels.copyPixels(_pixels,_flashRect,_flashPointZero);
frames = (_flashRect2.width / _flashRect.width) * (_flashRect2.height / _flashRect.height);
if(_colorTransform != null) framePixels.colorTransform(_flashRect,_colorTransform);
_curIndex = 0;
_numFrames = 0;

var widthHelper:uint = _flipped?_flipped:_pixels.width;
var maxFramesX:uint = FlxU.ceil(widthHelper / frameWidth);
var maxFramesY:uint = FlxU.ceil(_pixels.height / frameHeight);
_maxFrames = maxFramesX * maxFramesY;
}

/**
Expand Down Expand Up @@ -577,8 +589,8 @@ package org.flixel
}
else
_curFrame++;
_curIndex = _curAnim.frames[_curFrame];
dirty = true;
if (!trySetIndex(_curAnim.frames[_curFrame]))
FlxG.log("WARNING: A FlxSprite animation is trying to set the frame number of its FlxSprite out of bounds.");
}
}

Expand Down Expand Up @@ -645,8 +657,9 @@ package org.flixel
finished = true;
else
finished = false;
_curIndex = _curAnim.frames[_curFrame];
dirty = true;
if (!trySetIndex(_curAnim.frames[_curFrame]))
FlxG.log("WARNING: A FlxSprite animation is trying to set the frame number of its FlxSprite out of bounds.");

return;
}
i++;
Expand All @@ -661,8 +674,7 @@ package org.flixel
public function randomFrame():void
{
_curAnim = null;
_curIndex = int(FlxG.random()*(_pixels.width/frameWidth));
dirty = true;
trySetIndex(int(FlxG.random()*numFrames)); // Shouldn't ever throw an error
}

/**
Expand Down Expand Up @@ -827,11 +839,92 @@ package org.flixel
*/
public function set frame(Frame:uint):void
{
if (Frame >= numFrames)
{
FlxG.log("WARNING: The frame number of a FlxSprite must be less than its `numFrames` value.");
Frame = numFrames - 1;
}

_curAnim = null;
_curIndex = Frame;
dirty = true;
}

/**
* Try setting the `_curIndex` value to the specified value, extracted out to avoid duplicate code.
* If it is outside of the allowed bounds, it will still set the variable to the nearest
* possible value, but will return false, allowing internal code to throw its own error messages (if necessary).
* Will re-draw even if the frame hasn't changed (Adam had it that way, I'll just assume he did that on purpose).
*/
private function trySetIndex(Value:uint):Boolean
{
_curIndex = Value;
dirty = true;

if (_curIndex >= numFrames)
{
_curIndex = numFrames;
return false;
}

//else
return true;
}

/**
* The maximum number of frames that can fit on the sprite sheet, calculated based on the size of the each frame and the
*/
public function get maxFrames():uint
{
return _maxFrames;
}

/**
* The number of frames that are on the sprite sheet.
*
* @deprecated This property is deprecated. Use <code>numFrames</code> instead.
*/
public function get frames():uint
{
return numFrames;
}

/**
* The number of frames that are on the sprite sheet. Defaults to <code>maxFrames</code> if no value is set.
*
* @param NumFrames The number of frames on the sprite sheet. Has to be a value between <code>1</code> and <code>maxFrames</code>.
*/
public function get numFrames():uint
{
return (_numFrames == 0) ? maxFrames : _numFrames;
}

/**
* @private
*/
public function set numFrames(NumFrames:uint):void
{
if (NumFrames < 1)
{
FlxG.log("ERROR: Cannot set the number of frames on a FlxSprite to less than 1.");
_numFrames = 1;
return;
}

if (NumFrames > maxFrames)
{
FlxG.log("ERROR: Cannot set the number of frames on a FlxSprite to higher than its `maxFrames` value (" + maxFrames + ").");
_numFrames = maxFrames;
return;
}

// Will only re-render if the current frame number has changed
if (frame >= _numFrames)
{
frame = _numFrames - 1;
}
}

/**
* Check and see if this object is currently on screen.
* Differs from <code>FlxObject</code>'s implementation
Expand Down Expand Up @@ -917,4 +1010,4 @@ package org.flixel
dirty = false;
}
}
}
}
2 changes: 1 addition & 1 deletion org/flixel/FlxTileblock.as
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ package org.flixel
var sprite:FlxSprite = new FlxSprite().loadGraphic(TileGraphic,true,false,TileWidth,TileHeight);
var spriteWidth:uint = sprite.width;
var spriteHeight:uint = sprite.height;
var total:uint = sprite.frames + Empties;
var total:uint = sprite.numFrames + Empties;

//Then prep the "canvas" as it were (just doublechecking that the size is on tile boundaries)
var regen:Boolean = false;
Expand Down