hey i have a problem with Collision Detection i used this example from a website http://www.freeactionscript.com/2009/05/pixel-perfect-collision-detection/ but it only works when your Object's registration point is on the upper left corner what should i change in the code if let's say the registration point is on the center?
here is the code:
package
{
import flash.display.BitmapData;
import flash.display.DisplayObjectContainer;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
public class CollisionTest
{
// vars
private var _returnValue:Boolean;
private var _onePoint:Point;
private var _twoPoint:Point;
private var _oneRectangle:Rectangle;
private var _twoRectangle:Rectangle;
private var _oneClipBmpData:BitmapData;
private var _twoClipBmpData:BitmapData;
private var _oneOffset:Matrix;
private var _twoOffset:Matrix;
/**
* Simple collision test. Use this for objects that are not rotated.
* #param clip1 Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
* #param clip2 Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
* #return Collision True/False
*/
public function simple(clip1:DisplayObjectContainer, clip2:DisplayObjectContainer):Boolean
{
_returnValue = false;
_oneRectangle = clip1.getBounds(clip1);
_oneClipBmpData = new BitmapData(_oneRectangle.width, _oneRectangle.height, true, 0);
_oneClipBmpData.draw(clip1);
_twoRectangle = clip2.getBounds(clip2);
_twoClipBmpData = new BitmapData(_twoRectangle.width, _twoRectangle.height, true, 0);
_twoClipBmpData.draw(clip2);
_onePoint = new Point(clip1.x, clip1.y)
_twoPoint = new Point(clip2.x, clip2.y)
if (_oneClipBmpData.hitTest(_onePoint, 255, _twoClipBmpData, _twoPoint, 255))
{
_returnValue = true;
}
return _returnValue;
}
/**
* Complex collision test. Use this for objects that are rotated, scaled, skewed, etc
* #param clip1 Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
* #param clip2 Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
* #return Collision True/False
*/
public function complex(clip1:DisplayObjectContainer, clip2:DisplayObjectContainer):Boolean
{
_returnValue = false;
_twoRectangle = clip1.getBounds(clip1);
_oneOffset = clip1.transform.matrix;
_oneOffset.tx = clip1.x - clip2.x;
_oneOffset.ty = clip1.y - clip2.y;
_twoClipBmpData = new BitmapData(_twoRectangle.width, _twoRectangle.height, true, 0);
_twoClipBmpData.draw(clip1, _oneOffset);
_oneRectangle = clip2.getBounds(clip2);
_oneClipBmpData = new BitmapData(_oneRectangle.width, _oneRectangle.height, true, 0);
_twoOffset = clip2.transform.matrix;
_twoOffset.tx = clip2.x - clip2.x;
_twoOffset.ty = clip2.y - clip2.y;
_oneClipBmpData.draw(clip2, _twoOffset);
_onePoint = new Point(_oneRectangle.x, _oneRectangle.y);
_twoPoint = new Point(_twoRectangle.x, _twoRectangle.y);
if(_oneClipBmpData.hitTest(_onePoint, 255, _twoClipBmpData, _twoPoint, 255))
{
_returnValue = true;
}
_twoClipBmpData.dispose();
_oneClipBmpData.dispose();
return _returnValue;
}
}
}
Well lets put it like this: How far away is the top left corner from the center point?
x += width * .5;
y += height * .5;
Right?
So if the code you posted above only treats it with top left (for whatever reason I don't know why) why not force it to treat it with center point instead?
Perhaps something like this?
_onePoint = new Point(clip1.x + _clip.width * .5, clip1.y + _clip.height * .5);
_twoPoint = new Point(clip2.x + _clip2.width * .5, clip2.y + _clip2.height * .5);
Related
I’m trying to make a simple game that uses a basic equation:
‘linkCount’ / ‘clickCount’ * 100 to create a % success amount, ‘percentCount’
The text fields and the following as3 all live in a Movieclip on root level with instance name ‘counter_int’:
as3:
var clickCount:int = 1; //see below* for what controls this number
var linkCount:int = 1; //see below* for what controls this number
var percentCount:int = (100);
percentCount++;
percent_text.text = (int(linkCount) / int(clickCount) * 100 + "%").toString();
This works fine and displays a % amount in the correct field. However, my question is about truncating the % I get to remove anything after the decimal place. I’ve tried everything I can to get this to work but it’s not having it.
*
Now, here’s the tricky bit that i think is possibly causing my Math.round problem… I basically just don’t know where or how to apply the Math.round instruction?! I also suspect it might be a problem with using ‘int’ and have tried using ‘Number’ but it still displays decimal places.
I am using 2 buttons within 25 different movieclips…
Button locations:
all_int_circles_master.cone.FAILlinkbutton
all_int_circles_master.cone.linkbutton
all_int_circles_master.ctwo.FAILlinkbutton
all_int_circles_master.ctwo.linkbutton
etc … to ctwentyfive
The as3 on FAIL buttons:
FAILlinkbutton.addEventListener(MouseEvent.CLICK, addClick1);
function addClick1(event:MouseEvent):void
{
Object(root).counter_int.clickCount++;
Object(this.parent.parent).counter_int.clicked_total.text = Object(root).counter_int.clickCount.toString();
}
The as3 on successful link buttons:
linkbutton.addEventListener(MouseEvent.CLICK, onClickNextSlide2);
function onClickNextSlide2(event:MouseEvent):void
{
Object(root).counter_int.clickCount++;
Object(this.parent.parent).counter_int.clicked_total.text = Object(root).counter_int.clickCount.toString();
}
The % currently gets returned as e.g.:
74.334753434
but I need it to just be:
74
Any help would be greatly appreciated. I can supply the .fla if necessary. This is kind of what I've been trying but no luck so far:
should the Math.round be applied at root level / globally somehow!?
should the Math.round be applied within the counter_int movieclip?
should the Math.round be applied within all of the all_int_circles_master.cone / two / three... movieclips?
Thanks
Have you tried
percent_text.text = (Math.round(int(linkCount) / int(clickCount) * 100) + "%").toString();
percent_text.text = (int(linkCount) / int(clickCount) * 100 +
"%").toString();
I think you have your clickCount and linkCount backwards, swap those and round the results before adding the percent symbol:
percent_text.text = (Math.round((int(clickCount) / int(linkCount) * 100)).toString() + "%";
A pure AS3 example:
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
public class Main extends Sprite {
var clickCount:int = 0;
var linkCount:int = 30;
var clicked_total:TextField;
var button:CustomSimpleButton;
public function Main() {
button = new CustomSimpleButton();
button.addEventListener(MouseEvent.CLICK, onClickNextSlide2);
addChild(button);
clicked_total = new TextField();
clicked_total.text = "0%";
clicked_total.x = 100;
addChild(clicked_total);
}
function onClickNextSlide2(event:MouseEvent):void {
clickCount++;
if (clickCount < linkCount) {
clicked_total.text = (Math.round((clickCount / linkCount) * 100)).toString() + "%";
} else {
clicked_total.text = "Done";
}
}
}
}
import flash.display.Shape;
import flash.display.SimpleButton;
class CustomSimpleButton extends SimpleButton {
private var upColor:uint = 0xFFCC00;
private var overColor:uint = 0xCCFF00;
private var downColor:uint = 0x00CCFF;
private var size:uint = 80;
public function CustomSimpleButton() {
downState = new ButtonDisplayState(downColor, size);
overState = new ButtonDisplayState(overColor, size);
upState = new ButtonDisplayState(upColor, size);
hitTestState = new ButtonDisplayState(upColor, size * 2);
hitTestState.x = -(size / 4);
hitTestState.y = hitTestState.x;
useHandCursor = true;
}
}
class ButtonDisplayState extends Shape {
private var bgColor:uint;
private var size:uint;
public function ButtonDisplayState(bgColor:uint, size:uint) {
this.bgColor = bgColor;
this.size = size;
draw();
}
private function draw():void {
graphics.beginFill(bgColor);
graphics.drawRect(0, 0, size, size);
graphics.endFill();
}
}
I have small Movie clips that needs to be played, throughout the game i am making. Currently i am making them in aftereffets and exporting them as FLV files and embedding them into timeline, making a moviclip symbol to play them.
But that obviously is a disaster. This way i get very small movie clips but they don't garbage collect at all. So it keeps getting stored in the memory. if I turn them into SWF, the files become like 10 times bigger, going as high as 24 mb for 2.4 mb flv file. It's really frustrating to work with it this way.
Can somebody please suggest a way to work with videos/clips in actionscript 3? I don't need any controls for these movie clips, they serve as cut scenes.
Adobe introduced StageVideo, leveraging hardware acceleration for high performance video playback.
To mitigate the performance impact of rendering video in the Video
object, Adobe has introduced stage video as a new way to render video.
This approach takes full advantage of the underlying video hardware.
The result is a much lower load on the CPU, which translates into
higher frame rates on less powerful devices and also less memory
usage.
Example implementation from Thibault Imbert:
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageDisplayState;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.FullScreenEvent;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.StageVideoAvailabilityEvent;
import flash.events.StageVideoEvent;
import flash.events.TimerEvent;
import flash.events.VideoEvent;
import flash.geom.Rectangle;
import flash.media.SoundTransform;
import flash.media.StageVideo;
import flash.media.StageVideoAvailability;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.system.LoaderContext;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.ui.Keyboard;
/**
*
* #author Thibault Imbert
*
*/
[SWF(frameRate="1", backgroundColor="#000000")]
public class SimpleStageVideo extends Sprite
{
private static const FILE_NAME:String = "video-file.mov";
private static const INTERVAL:Number = 500;
private static const BORDER:Number = 20;
private var legend:TextField = new TextField();
private var sv:StageVideo;
private var nc:NetConnection;
private var ns:NetStream;
private var rc:Rectangle;
private var video:Video;
private var thumb:Shape;
private var interactiveThumb:Sprite;
private var totalTime:Number;
private var videoWidth:int;
private var videoHeight:int;
private var outputBuffer:String = new String();
private var rect:Rectangle = new Rectangle(0, 0, 0, BORDER);
private var videoRect:Rectangle = new Rectangle(0, 0, 0, 0);
private var gotStage:Boolean;
private var stageVideoInUse:Boolean;
private var classicVideoInUse:Boolean;
private var accelerationType:String;
private var infos:String = new String();
private var available:Boolean;
private var inited:Boolean;
private var played:Boolean;
private var container:Sprite;
/**
*
*
*/
public function SimpleStageVideo()
{
// Make sure the app is visible and stage available
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
/**
*
* #param event
*
*/
private function onAddedToStage(event:Event):void
{
// Scaling
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
legend.autoSize = TextFieldAutoSize.LEFT;
// Debug infos
legend.multiline = true;
legend.background = true;
legend.backgroundColor = 0xFFFFFFFF;
addChild(legend);
// Thumb seek Bar
thumb = new Shape();
interactiveThumb = new Sprite();
interactiveThumb.addChild(thumb);
addChild(interactiveThumb);
// Connections
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
ns.client = this;
// Screen
video = new Video();
video.smoothing = true;
// Video Events
// the StageVideoEvent.STAGE_VIDEO_STATE informs you if StageVideo is available or not
stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, onStageVideoState);
// in case of fallback to Video, we listen to the VideoEvent.RENDER_STATE event to handle resize properly and know about the acceleration mode running
video.addEventListener(VideoEvent.RENDER_STATE, videoStateChange);
// Input Events
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(Event.RESIZE, onResize);
stage.addEventListener(MouseEvent.CLICK, onClick);
}
/**
*
* #param event
*
*/
private function onNetStatus(event:NetStatusEvent):void
{
if ( event.info == "NetStream.Play.StreamNotFound" )
legend.text = "Video file passed, not available!";
}
/**
*
* #param event
*
*/
private function onFrame(event:Event):void
{
var ratio:Number = (ns.time / totalTime) * (stage.stageWidth - (BORDER << 1));
rect.width = ratio;
thumb.graphics.clear();
thumb.graphics.beginFill(0xFFFFFF);
thumb.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
}
/**
*
* #param event
*
*/
private function onClick(event:MouseEvent):void
{
if ( event.stageY >= interactiveThumb.y - BORDER && event.stageX <= stage.stageWidth - BORDER )
{
var seekTime:Number = (stage.mouseX - BORDER) * ( totalTime / (stage.stageWidth - (BORDER << 1) ) );
ns.seek( seekTime );
}
}
/**
*
* #param event
*
*/
private function onKeyDown(event:KeyboardEvent):void
{
if ( event.keyCode == Keyboard.O )
{
if ( available )
// We toggle the StageVideo on and off (fallback to Video and back to StageVideo)
toggleStageVideo(inited=!inited);
} else if ( event.keyCode == Keyboard.F )
{
stage.displayState = StageDisplayState.FULL_SCREEN;
} else if ( event.keyCode == Keyboard.SPACE )
{
ns.togglePause();
}
}
/**
*
* #param width
* #param height
* #return
*
*/
private function getVideoRect(width:uint, height:uint):Rectangle
{
var videoWidth:uint = width;
var videoHeight:uint = height;
var scaling:Number = Math.min ( stage.stageWidth / videoWidth, stage.stageHeight / videoHeight );
videoWidth *= scaling, videoHeight *= scaling;
var posX:uint = stage.stageWidth - videoWidth >> 1;
var posY:uint = stage.stageHeight - videoHeight >> 1;
videoRect.x = posX;
videoRect.y = posY;
videoRect.width = videoWidth;
videoRect.height = videoHeight;
return videoRect;
}
/**
*
*
*/
private function resize ():void
{
if ( stageVideoInUse )
{
// Get the Viewport viewable rectangle
rc = getVideoRect(sv.videoWidth, sv.videoHeight);
// set the StageVideo size using the viewPort property
sv.viewPort = rc;
} else
{
// Get the Viewport viewable rectangle
rc = getVideoRect(video.videoWidth, video.videoHeight);
// Set the Video object size
video.width = rc.width;
video.height = rc.height;
video.x = rc.x, video.y = rc.y;
}
interactiveThumb.x = BORDER, interactiveThumb.y = stage.stageHeight - (BORDER << 1);
legend.text = infos;
}
/**
*
* #param evt
*
*/
public function onMetaData ( evt:Object ):void
{
totalTime = evt.duration;
stage.addEventListener(Event.ENTER_FRAME, onFrame);
}
/**
*
* #param event
*
*/
private function onStageVideoState(event:StageVideoAvailabilityEvent):void
{
// Detect if StageVideo is available and decide what to do in toggleStageVideo
toggleStageVideo(available = inited = (event.availability == StageVideoAvailability.AVAILABLE));
}
/**
*
* #param on
*
*/
private function toggleStageVideo(on:Boolean):void
{
infos = "StageVideo Running (Direct path) : " + on + "\n";
// If we choose StageVideo we attach the NetStream to StageVideo
if (on)
{
stageVideoInUse = true;
if ( sv == null )
{
sv = stage.stageVideos[0];
sv.addEventListener(StageVideoEvent.RENDER_STATE, stageVideoStateChange);
}
sv.attachNetStream(ns);
if (classicVideoInUse)
{
// If we use StageVideo, we just remove from the display list the Video object to avoid covering the StageVideo object (always in the background)
stage.removeChild ( video );
classicVideoInUse = false;
}
} else
{
// Otherwise we attach it to a Video object
if (stageVideoInUse)
stageVideoInUse = false;
classicVideoInUse = true;
video.attachNetStream(ns);
stage.addChildAt(video, 0);
}
if ( !played )
{
played = true;
ns.play(FILE_NAME);
}
}
/**
*
* #param event
*
*/
private function onResize(event:Event):void
{
resize();
}
/**
*
* #param event
*
*/
private function stageVideoStateChange(event:StageVideoEvent):void
{
infos += "StageVideoEvent received\n";
infos += "Render State : " + event.status + "\n";
resize();
}
/**
*
* #param event
*
*/
private function videoStateChange(event:VideoEvent):void
{
infos += "VideoEvent received\n";
infos += "Render State : " + event.status + "\n";
resize();
}
}
}
I am trying to place an object at my players current position but when i move away the object sticks to my player. I kind of know why it sticking to my player but i cant think of any other code to use.
Hero is my player that i move around the screen.
Thanks Lochy
var trap1:trap = new trap();
function keydown(event:KeyboardEvent) :void {
if(event.keyCode ==32)
addChild(trap1);
trap1.x = hero.x;
trap1.y = hero.y;
There are several ways to accomplish this task. There is basically a detail you have to know. In Flash, the display list is responsible for managing elements on the screen. The DisplayObject and DisplayObjectContainer classes provide the API to access and manipulate the display list.
A naive approach would be
function placeAbove(d1:DisplayObject, d2:DisplayObject):void
{
if (!d1 || !d2) return;
d1.x = d2.x;
d1.y = d2.y;
}
But when both DisplayObjects have different parents, the DisplayObjects are not part of te same coordinate system, so this little method won't work. I coded a small example:
package
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.geom.Point;
public class Points extends Sprite
{
public function Points()
{
const child1:Sprite = createSquare(50, 50, 0xFF0000, .5)
, child2:Sprite = createSquare(100, 100, 0x0000FF, .5)
, container1:Sprite = new Sprite()
, container2:Sprite = new Sprite();
DisplayObjectContainer(placeRandomly(addChild(container1))).addChild(child1);
DisplayObjectContainer(placeRandomly(addChild(container2))).addChild(child2);
placeAbove(child1, child2);
}
private function createSquare(width:Number, height:Number, color:uint = 0, alpha:Number = 1):Sprite
{
const sprite:Sprite = new Sprite();
sprite.graphics.beginFill(color, alpha);
sprite.graphics.drawRect(0, 0, width, height);
sprite.graphics.endFill();
return sprite;
}
private function placeAbove(child1:Sprite, child2:Sprite):void
{
const point:Point = child2.localToGlobal(new Point(0, 0))
, point2:Point = child1.globalToLocal(point);
child1.x = point2.x;
child1.y = point2.y;
}
private function placeRandomly(displayObject:DisplayObject):DisplayObject
{
displayObject.x = Math.random() * 100;
displayObject.y = Math.random() * 100;
return displayObject;
}
}
}
The Application generate 2 squares and adds it into different parents. The containers (parents) are placed randomly on the screen and so are the two child display objects. The placeAbove method does all the magic. It calculates the position from the second display object globally and the maps it to the first display objects target local position within the parent's coordinate system.
Ii hope it helps.
It's not very clear how your display list is organized, but try adding the trap as a child of the hero's parent. i.e.:
hero.parent.addChild(trap1);
I want to create a image magnify application like following:
A masked small window showig big image area corresponding to the mouse X and Y on the small image. There are many magnifying image application exaples online such as:
http://www.flashandmath.com/intermediate/magglass/mag_glass.html
But here the mouse and mask moves with same X and Y. What i want is that masked window display only certain area corresponding to mouse X and Y on Small image.
Any help would be highly appreciated. thanks.
i wrote a recipe last year for exactly what you're looking for. i do not guarantee that's it's as refactored or efficient as it could be, but it works really well. change it up as much as you like. i post the code hear for anyone to freely use.
however, the photograph and loupe asset i do not permit anyone to use without prior request, please.
the class lets you alter your own magnification strength, even at runtime if you want. you can use your own loupe graphic, but one is also included in the source files (please ask me first if you want to use it in your project).
Description:
Magnifier: Creating A Customizable
Magnifier For Image Assets
The following code demonstrates the
solution for creating a customizable
magnifier for image assets using the
Magnifier class.
The Magnifier constructor receives 6
parameters. The first
loupeDisplayObject:DisplayObject
required parameter is a reference to a
display object that is used as the
virtual loupe. In order for the class
to function properly, the
loupeDisplayObject:DisplayObject must
contain a circular or elliptically
shaped void or alpha transparency at
its center.
The second imageURL:String required
parameter supplies the URLLoader’s
load function’s URLRequest with the
URL of the target image asset. The
image provides BitmapData for both
thumbSprite:Sprite and
magnificationSprite:Sprite objects,
which are scaled using the third
thumbScale:Number and fourth
magnificationScale:Number optional
parameters. The scale of the
thumbSprite:Sprite is exhibited on
stage, while the scale of the
magnificationSprite:Sprite is visible
during magnification.
The Magnifier class operates by
employing mouse events to toggle the
visibility of a virtual loupe over an
image asset. A maskSprite:Sprite
ellipse, both indexed below and based
on the size of the
loupeDisplayObject:DisplayObject, is
created to mask the
magnificationSprite:Sprite. However,
the fifth maskWidth:Number and sixth
maskHeight:Number optional parameters
can be set to manually size a
maskSprite:Sprite that is more
suitable for a
loupeDisplayObject:DisplayObject with
a complex shape.
Calling the public deallocate()
function of the Magnifier instance
prior to its nullification will mark
it as being available for garbage
collection.
Class FIle:
package
{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.geom.Matrix;
import flash.net.URLRequest;
import flash.ui.Mouse;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.Regular;
public class Magnifier extends Sprite
{
//Class Variables
private var loupeDisplayObject:DisplayObject;
private var imageWidth:Number;
private var imageHeight:Number;
private var thumbScale:Number;
private var magnificationScale:Number;
private var maskWidth:Number;
private var maskHeight:Number;
private var imageBitmapData:BitmapData;
private var maskSprite:Sprite;
private var magnificationSprite:Sprite;
private var thumbSprite:Sprite;
private var loupeTween:Tween;
private var magnificationTween:Tween;
//Constructor
public function Magnifier (
loupeDisplayObject:DisplayObject,
imageURL:String,
thumbScale:Number = 0.5,
magnificationScale:Number = 1.0,
maskWidth:Number = NaN,
maskHeight:Number = NaN
)
{
this.loupeDisplayObject = loupeDisplayObject;
this.thumbScale = Math.max(0.1, Math.min(thumbScale, 1.0));
this.magnificationScale = Math.max(0.1, magnificationScale);
this.maskWidth = maskWidth;
this.maskHeight = maskHeight;
init(imageURL);
}
//Load And Handle Image
private function init(imageURL:String):void
{
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageHandler);
imageLoader.load(new URLRequest(imageURL));
}
private function errorHandler(evt:IOErrorEvent):void
{
throw(evt.text);
}
private function imageHandler(evt:Event):void
{
evt.target.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
evt.target.removeEventListener(Event.COMPLETE, imageHandler);
imageWidth = evt.target.content.width;
imageHeight = evt.target.content.height;
imageBitmapData = new BitmapData(imageWidth, imageHeight);
imageBitmapData.draw(evt.target.content);
createComponents();
}
//Create Components
private function createComponents():void
{
//Loupe Visibility
loupeDisplayObject.alpha = 0;
//Mask
if (isNaN(maskWidth)) maskWidth = loupeDisplayObject.width;
if (isNaN(maskHeight)) maskHeight = loupeDisplayObject.height;
maskSprite = new Sprite();
maskSprite.graphics.beginFill(0x00FF00, 0.5);
maskSprite.graphics.drawEllipse(0, 0, maskWidth, maskHeight);
maskSprite.graphics.endFill();
maskSprite.mouseEnabled = false;
//Magnification
magnificationSprite = scaleImage(new Matrix(magnificationScale, 0, 0, magnificationScale));
magnificationSprite.mouseEnabled = false;
magnificationSprite.alpha = 0;
magnificationSprite.mask = maskSprite;
//Thumb
thumbSprite = scaleImage(new Matrix(thumbScale, 0, 0, thumbScale));
thumbSprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
//Add Components To The Display List
addChild(thumbSprite);
addChild(magnificationSprite);
addChild(maskSprite);
addChild(loupeDisplayObject);
}
private function scaleImage(matrix:Matrix):Sprite
{
var scaledResult:Sprite = new Sprite();
scaledResult.graphics.beginBitmapFill(imageBitmapData, matrix, false, true);
scaledResult.graphics.drawRect(0, 0, imageWidth * matrix.a, imageHeight * matrix.d);
scaledResult.graphics.endFill();
return scaledResult;
}
//Mouse Event Handlers
private function mouseDownHandler(evt:MouseEvent):void
{
thumbSprite.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
thumbSprite.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
mouseMoveHandler(evt);
setLoupeAsVisible(true);
}
private function mouseMoveHandler(evt:MouseEvent):void
{
loupeDisplayObject.x = evt.localX - loupeDisplayObject.width / 2;
loupeDisplayObject.y = evt.localY - loupeDisplayObject.height / 2;
maskSprite.x = evt.localX - maskSprite.width / 2;
maskSprite.y = evt.localY - maskSprite.height / 2;
magnificationSprite.x = 0 - evt.localX / thumbSprite.width * (magnificationSprite.width - thumbSprite.width);
magnificationSprite.y = 0 - evt.localY / thumbSprite.height * (magnificationSprite.height - thumbSprite.height);
}
private function mouseOutHandler(evt:MouseEvent):void
{
thumbSprite.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
setLoupeAsVisible(false);
}
private function mouseOverHandler(evt:MouseEvent):void
{
thumbSprite.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
setLoupeAsVisible(true);
}
private function mouseUpHandler(evt:MouseEvent):void
{
if (thumbSprite.hasEventListener(MouseEvent.MOUSE_OVER)) thumbSprite.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
thumbSprite.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
thumbSprite.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
setLoupeAsVisible(false);
}
//Loupe Tween And Visibility
private function setLoupeAsVisible(response:Boolean):void
{
var targetAlpha:Number;
if (response)
{
targetAlpha = 1.0;
Mouse.hide();
}
else
{
targetAlpha = 0.0;
Mouse.show();
}
loupeTween = new Tween(loupeDisplayObject, "alpha", Regular.easeIn, loupeDisplayObject.alpha, targetAlpha, 0.25, true);
magnificationTween = new Tween(magnificationSprite, "alpha", Regular.easeIn, magnificationSprite.alpha, targetAlpha, 0.25, true);
}
//Clean Up
public function deallocate():void
{
thumbSprite.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}
}
}
I'm very very new on ActionScript 3.0 development for Blackberry Playbook.
I'm working with Double Sided 3D Plane in FP10 and I'm having troubles with this code:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class FlipCard extends Sprite
{
private var myPaperSprite:PaperSprite;
public function FlipCard()
{
super();
}
private function Flip():void
{
/*
Create a new PaperSprite:
If your front and back faces already exist, you can pass them straight
into the constructor, like so:
myPaperSprite = new PaperSprite( myFrontMc, myBackMc );
*/
myPaperSprite = new PaperSprite();
/*
Optionally specify a pivot point, in this example the centre is used
(this is also the default so there is no need to set this normally).
To pivot around the top left use:
myPaperSprite.pivot = new Point(0,0);
or for bottom right:
myPaperSprite.pivot = new Point(1,1);
and so on...
*/
myPaperSprite.pivot = new Point(0.5,0.5);
line myPaperSprite.pivot = new Point(0.5,0.5); is throwing the following error:
1180: Call to a possibly undefined method Point.
And pivot member is defined as follows:
package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
public class PaperSprite extends Sprite
{
//___________________________________________________________
//————————————————————————————————————————————— CLASS MEMBERS
private static const POINT_A:Point = new Point(0, 0);
private static const POINT_B:Point = new Point(100, 0);
private static const POINT_C:Point = new Point(0, 100);
private var _isFrontFacing:Boolean = true;
private var _autoUpdate:Boolean = true;
private var _pivot:Point = new Point(0.5,0.5);
private var _front:DisplayObject;
private var _back:DisplayObject;
private var _p1:Point;
private var _p2:Point;
private var _p3:Point;
//___________________________________________________________
//————————————————————————————————————————— GETTERS + SETTERS
/**
* A Point Object used to determine the pivot, or registration point of the
* PaperSprite. The values should be between 0 and 1 and are relative to the
* dimensions of each face, 0 being far left (on the x axis) or top (y axis)
* and 1 being the far right (x axis) or bottom (y axis).
*
* The default value is { x:0.5, y:0.5 } meaning that both faces will pivot
* around their respective centres
*
* Examples:
*
* To pivot from the centre use: new Point( 0.5, 0.5 );
* To pivot from the top left use: new Point( 0.0, 0.0 );
* To Pivot from the bottom right use: new Point( 1.0, 1.0 );
*
*/
public function get pivot():Point
{
return _pivot;
}
public function set pivot( value:Point ):void
{
_pivot = value;
alignFaces();
}
Where is the problem? What am I doing wrong?
add import flash.geom.Point to FlipCard.as as well, as it is not visible in that class