Skip to content

Commit

Permalink
improved button behavior in multi-camera environments, beefed up soun…
Browse files Browse the repository at this point in the history
…d reusability, added sound events to button
  • Loading branch information
AdamAtomic committed May 20, 2011
1 parent 0fa2087 commit 147f95a
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 49 deletions.
133 changes: 111 additions & 22 deletions org/flixel/FlxButton.as
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package org.flixel
public class FlxButton extends FlxSprite
{
[Embed(source="data/button.png")] protected var ImgDefaultButton:Class;
[Embed(source="data/beep.mp3")] protected var SndBeep:Class;

/**
* Used with public variable <code>status</code>, means not highlighted or pressed.
Expand All @@ -32,27 +33,54 @@ package org.flixel
* Controls the offset (from top left) of the text from the button.
*/
public var labelOffset:FlxPoint;
/**
* This function is called when the button is released.
* We recommend assigning your main button behavior to this function
* via the <code>FlxButton</code> constructor.
*/
public var onUp:Function;
/**
* This function is called when the button is pressed down.
*/
public var onDown:Function;
/**
* This function is called when the mouse goes over the button.
*/
public var onOver:Function;
/**
* This function is called when the mouse leaves the button area.
*/
public var onOut:Function;
/**
* Shows the current state of the button.
*/
public var status:uint;

/**
* Used for checkbox-style behavior.
* Set this to play a sound when the mouse goes over the button.
* We recommend using the helper function setSounds()!
*/
protected var _onToggle:Boolean;
public var soundOver:FlxSound;
/**
* This function is called when the button is clicked.
* Set this to play a sound when the mouse leaves the button.
* We recommend using the helper function setSounds()!
*/
protected var _onClick:Function;
public var soundOut:FlxSound;
/**
* This function is called when the mouse goes over the button.
* Set this to play a sound when the button is pressed down.
* We recommend using the helper function setSounds()!
*/
protected var _onOver:Function;
public var soundDown:FlxSound;
/**
* This function is called when the mouse leaves the button area.
* Set this to play a sound when the button is released.
* We recommend using the helper function setSounds()!
*/
public var soundUp:FlxSound;

/**
* Used for checkbox-style behavior.
*/
protected var _onOut:Function;
protected var _onToggle:Boolean;

/**
* Tracks whether or not the button is currently pressed.
*/
Expand All @@ -70,22 +98,27 @@ package org.flixel
* @param Y The Y position of the button.
* @param Label The text that you want to appear on the button.
* @param OnClick The function to call whenever the button is clicked.
* @param OnOver The function to call whenever the mouse goes over the button.
* @param OnOut The function to call whenever the mouse leaves the button area.
*/
public function FlxButton(X:Number=0,Y:Number=0,Label:String=null,OnClick:Function=null,OnOver:Function=null,OnOut:Function=null)
public function FlxButton(X:Number=0,Y:Number=0,Label:String=null,OnClick:Function=null)
{
super(X,Y);
_onClick = OnClick;
_onOver = OnOver;
_onOut = OnOut;
if(Label != null)
{
label = new FlxText(0,0,80,Label);
label.setFormat(null,8,0x333333,"center");
labelOffset = new FlxPoint(-1,3);
}
loadGraphic(ImgDefaultButton,true,false,80,20);

onUp = OnClick;
onDown = null;
onOut = null;
onOver = null;

soundOver = null;
soundOut = null;
soundDown = null;
soundUp = null;

status = NORMAL;
_onToggle = false;
Expand All @@ -105,7 +138,18 @@ package org.flixel
label.destroy();
label = null;
}
_onClick = null;
onUp = null;
onDown = null;
onOut = null;
onOver = null;
if(soundOver != null)
soundOver.destroy();
if(soundOut != null)
soundOut.destroy();
if(soundDown != null)
soundDown.destroy();
if(soundUp != null)
soundUp.destroy();
super.destroy();
}

Expand Down Expand Up @@ -178,19 +222,32 @@ package org.flixel
{
offAll = false;
if(FlxG.mouse.justPressed())
{
status = PRESSED;
if(onDown != null)
onDown();
if(soundDown != null)
soundDown.play(true);
}
if(status == NORMAL)
{
status = HIGHLIGHT;
if(_onOver != null)
_onOver();
if(onOver != null)
onOver();
if(soundOver != null)
soundOver.play(true);
}
}
}
if(offAll)
{
if((status != NORMAL) && (_onOut != null))
_onOut();
if(status != NORMAL)
{
if(onOut != null)
onOut();
if(soundOut != null)
soundOut.play(true);
}
status = NORMAL;
}
}
Expand All @@ -201,6 +258,7 @@ package org.flixel
{
label.x = x;
label.y = y;
label.scrollFactor = scrollFactor;
}
if(labelOffset != null)
{
Expand Down Expand Up @@ -235,6 +293,33 @@ package org.flixel
label.width = width;
}

/**
* Set sounds to play during mouse-button interactions.
* These operations can be done manually as well, and the public
* sound variables can be used after this for more fine-tuning,
* such as positional audio, etc.
*
* @param SoundOver What embedded sound effect to play when the mouse goes over the button. Default is null, or no sound.
* @param SoundOverVolume How load the that sound should be.
* @param SoundOut What embedded sound effect to play when the mouse leaves the button area. Default is null, or no sound.
* @param SoundOutVolume How load the that sound should be.
* @param SoundDown What embedded sound effect to play when the mouse presses the button down. Default is null, or no sound.
* @param SoundDownVolume How load the that sound should be.
* @param SoundUp What embedded sound effect to play when the mouse releases the button. Default is null, or no sound.
* @param SoundUpVolume How load the that sound should be.
*/
public function setSounds(SoundOver:Class=null, SoundOverVolume:Number=1.0, SoundOut:Class=null, SoundOutVolume:Number=1.0, SoundDown:Class=null, SoundDownVolume:Number=1.0, SoundUp:Class=null, SoundUpVolume:Number=1.0):void
{
if(SoundOver != null)
soundOver = FlxG.loadSound(SoundOver, SoundOverVolume);
if(SoundOut != null)
soundOut = FlxG.loadSound(SoundOut, SoundOutVolume);
if(SoundDown != null)
soundDown = FlxG.loadSound(SoundDown, SoundDownVolume);
if(SoundUp != null)
soundUp = FlxG.loadSound(SoundUp, SoundUpVolume);
}

/**
* Use this to toggle checkbox-style behavior.
*/
Expand All @@ -256,8 +341,12 @@ package org.flixel
*/
protected function onMouseUp(event:MouseEvent):void
{
if(exists && visible && active && (status == PRESSED) && (_onClick != null))
_onClick();
if(!exists || !visible || !active || (status != PRESSED))
return;
if(onUp != null)
onUp();
if(soundUp != null)
soundUp.play(true);
}
}
}
16 changes: 16 additions & 0 deletions org/flixel/FlxCamera.as
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,22 @@ package org.flixel
_flashSprite.scaleY = Y;
}

/**
* Fetches a reference to the Flash <code>Sprite</code> object
* that contains the camera display in the Flash display list.
* Uses include 3D projection, advanced display list modification, and more.
* NOTE: We don't recommend modifying this directly unless you are
* fairly experienced. For simple changes to the camera display,
* like scaling, rotation, and color tinting, we recommend
* using the existing <code>FlxCamera</code> variables.
*
* @return A Flash <code>Sprite</code> object containing the camera display.
*/
public function getContainerSprite():Sprite
{
return _flashSprite;
}

/**
* Fill the camera with the specified color.
*
Expand Down
55 changes: 41 additions & 14 deletions org/flixel/FlxG.as
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ package org.flixel
* Assign a minor version to your library.
* Appears after the decimal in the console.
*/
static public var LIBRARY_MINOR_VERSION:uint = 53;
static public var LIBRARY_MINOR_VERSION:uint = 54;

/**
* Debugger overlay layout preset: Wide but low windows at the bottom of the screen.
Expand Down Expand Up @@ -501,39 +501,65 @@ package org.flixel
}

/**
* Creates a new sound object from an embedded <code>Class</code> object.
* Creates a new sound object.
*
* @param EmbeddedSound The sound you want to play.
* @param EmbeddedSound The embedded sound resource you want to play. To stream, use the optional URL parameter instead.
* @param Volume How loud to play it (0 to 1).
* @param Looped Whether or not to loop this sound.
* @param Looped Whether to loop this sound.
* @param AutoDestroy Whether to destroy this sound when it finishes playing. Leave this value set to "false" if you want to re-use this <code>FlxSound</code> instance.
* @param AutoPlay Whether to play the sound.
* @param URL Load a sound from an external web resource instead. Only used if EmbeddedSound = null.
*
* @return A <code>FlxSound</code> object.
*/
static public function play(EmbeddedSound:Class,Volume:Number=1.0,Looped:Boolean=false):FlxSound
static public function loadSound(EmbeddedSound:Class=null,Volume:Number=1.0,Looped:Boolean=false,AutoDestroy:Boolean=false,AutoPlay:Boolean=false,URL:String=null):FlxSound
{
if((EmbeddedSound == null) && (URL == null))
{
FlxG.log("WARNING: FlxG.loadSound() requires either\nan embedded sound or a URL to work.");
return null;
}
var sound:FlxSound = sounds.recycle(FlxSound) as FlxSound;
sound.loadEmbedded(EmbeddedSound,Looped);
if(EmbeddedSound != null)
sound.loadEmbedded(EmbeddedSound,Looped,AutoDestroy);
else
sound.loadStream(URL,Looped,AutoDestroy);
sound.volume = Volume;
sound.play();
if(AutoPlay)
sound.play();
return sound;
}

/**
* Creates a new sound object from an embedded <code>Class</code> object.
* NOTE: Just calls FlxG.loadSound() with AutoPlay == true.
*
* @param EmbeddedSound The sound you want to play.
* @param Volume How loud to play it (0 to 1).
* @param Looped Whether to loop this sound.
* @param AutoDestroy Whether to destroy this sound when it finishes playing. Leave this value set to "false" if you want to re-use this <code>FlxSound</code> instance.
*
* @return A <code>FlxSound</code> object.
*/
static public function play(EmbeddedSound:Class,Volume:Number=1.0,Looped:Boolean=false,AutoDestroy:Boolean=true):FlxSound
{
return FlxG.loadSound(EmbeddedSound,Volume,Looped,AutoDestroy,true);
}

/**
* Creates a new sound object from a URL.
* NOTE: Just calls FlxG.loadSound() with AutoPlay == true.
*
* @param URL The URL of the sound you want to play.
* @param Volume How loud to play it (0 to 1).
* @param Looped Whether or not to loop this sound.
* @param AutoDestroy Whether to destroy this sound when it finishes playing. Leave this value set to "false" if you want to re-use this <code>FlxSound</code> instance.
*
* @return A FlxSound object.
*/
static public function stream(URL:String,Volume:Number=1.0,Looped:Boolean=false):FlxSound
static public function stream(URL:String,Volume:Number=1.0,Looped:Boolean=false,AutoDestroy:Boolean=true):FlxSound
{
var sound:FlxSound = sounds.recycle(FlxSound) as FlxSound;
sound.loadStream(URL,Looped);
sound.volume = Volume;
sound.play();
return sound;
return FlxG.loadSound(null,Volume,Looped,AutoDestroy,true,URL);
}

/**
Expand Down Expand Up @@ -721,6 +747,7 @@ package org.flixel
mtx.translate(newPixels.width,0);
newPixels.draw(pixels,mtx);
pixels = newPixels;
_cache[Key] = pixels;
}
return pixels;
}
Expand Down Expand Up @@ -863,7 +890,7 @@ package org.flixel
* @param OnComplete A function you want to run when the fade finishes.
* @param Force Force the effect to reset.
*/
static public function fade(Color:uint=0xffffffff, Duration:Number=1, OnComplete:Function=null, Force:Boolean=false):void
static public function fade(Color:uint=0xff000000, Duration:Number=1, OnComplete:Function=null, Force:Boolean=false):void
{
var i:uint = 0;
var l:uint = FlxG.cameras.length;
Expand Down
Loading

0 comments on commit 147f95a

Please sign in to comment.