Actionscript 3.0 Keyboard event listener error - actionscript-3

I have two functions, rotate and unrotate, and I used a keyboard event listener to listen for key_Down A and B but i get and error:
1119: Access of possibly undefined property A through a reference with static type Class.
ti.border = true
ti.addEventListener(TextEvent.TEXT_INPUT, onInput);
function onInput(event:TextEvent):void {
if(ti.text.search('a')!=-1) load_image("http://i54.tinypic.com/anom5d.png", "ottefct");
else if(ti.text.search('b')!=-1) load_image("http://i53.tinypic.com/2dv7dao.png", "rnd");
else if(ti.text.search('c')!=-1) load_image("http://i51.tinypic.com/m8jp7m.png", "ssd");
}
var loaded_images:Dictionary = new Dictionary();
function load_image(url:String, id_name:String)
{
var loader:Loader = new Loader();
loader.name = id_name;
var url_req:URLRequest = new URLRequest(url);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
loader.load(url_req);
}
function onLoadingComplete(evt:Event):void
{
var img_name:String = evt.currentTarget.loader.name
var spr_box:Sprite = new Sprite();
spr_box.addChild(evt.currentTarget.loader);
spr_box.mouseChildren = false;
spr_box.doubleClickEnabled = true;
spr_box.addEventListener(MouseEvent.MOUSE_DOWN, drag);
spr_box.addEventListener(MouseEvent.MOUSE_UP, drop);
spr_box.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
spr_box.addEventListener(KeyboardEvent.KEY_DOWN, unrotate);
spr_box.width = 124;
spr_box.height = 180;
spr_box.x = 430;
spr_box.y = 425;
this.addChild(spr_box);
loaded_images[img_name] = spr_box;
}
function drag(evt:MouseEvent):void
{
evt.currentTarget.startDrag()
}
function drop(evt:MouseEvent):void
{
evt.currentTarget.stopDrag()
}
function rotate(evt:KeyboardEvent):void
{
if (evt.keyCode==Keyboard.D) {
evt.currentTarget.rotation = 90
}
}
function unrotate(evt:KeyboardEvent):void
{
if (evt.keyCode==Keyboard.A) {
evt.currentTarget.rotation = 0
}
}

Well - you are only referencing prop A in one spot and its throwing an undefined error. So, either your not linking the Keyboard class properly... or...?
At any rate - you can also use numeric assignment to capture keystrokes. In this case:
function rotate(evt:KeyboardEvent):void
{
if (evt.keyCode == 68) { //"D"
evt.currentTarget.rotation = 90
}
}
function unrotate(evt:KeyboardEvent):void
{
if (evt.keyCode == 65) { //"A"
evt.currentTarget.rotation = 0
}
}
They are all laid out pretty clearly right here.

Related

Disable stop the object when it is hit on the right position

I want to be a drag and drop game.When I will to start drag my object be a small size to a drop position.It is performed correctly.But when drag object is touched hit point, it could not stop drag. Can I stop drag?Here is my code.....
drag_6.buttonMode = true;
drag_6.addEventListener(MouseEvent.MOUSE_UP, dropMe6);
drag_6.addEventListener(MouseEvent.MOUSE_DOWN, dragMe6);
var back_6X:Number = back_6.x;
var back_6Y:Number = back_6.y;
var hit_6X:Number = hit_6.x;
var hit_6Y:Number = hit_6.y;
var drop_6H:Number = drop_6.height;
var drop_6W:Number = drop_6.width
function dragMe6(event:MouseEvent)
{
drag_6.startDrag();
drag_6.height=drop_6H;
drag_6.width=drop_6W;
setChildIndex(drag_6, this.numChildren-1);
}
function dropMe6(event:MouseEvent)
{
drag_6.stopDrag();
if (drag_6.hitTestObject(drop_6))
{
TweenMax.to(drag_6, 0.5, {x:hit_6X, y:hit_6Y, ease:Cubic.easeOut});
drag_6.mouseEnabled = false;
SoundMixer.stopAll();
drag_6.alpha = 0 ;
hit_6.alpha = 1;
drag_6.buttonMode = false;
}
else
{
TweenMax.to(drag_6, 0.5, {x:back_6X, y:back_6Y, ease:Bounce.easeOut});
SoundMixer.stopAll();
}
}
The reason is the mouseup event is not firing at all. The best a simple way to add event listeners to stage. `
drag_6.buttonMode = true;
drag_6.addEventListener(MouseEvent.MOUSE_DOWN, dragMe6);
var back_6X:Number = back_6.x;
var back_6Y:Number = back_6.y;
var hit_6X:Number = hit_6.x;
var hit_6Y:Number = hit_6.y;
var drop_6H:Number = drop_6.height;
var drop_6W:Number = drop_6.width
function dragMe6(event:MouseEvent)
{
stage.addEventListener(MouseEvent.MOUSE_UP, dropMe6);
drag_6.startDrag();
drag_6.height=drop_6H;
drag_6.width=drop_6W;
setChildIndex(drag_6, this.numChildren-1);
}
function dropMe6(event:MouseEvent)
{
stage.removeEventListener(MouseEvent.MOUSE_UP, dropMe6);
drag_6.stopDrag();
if (drag_6.hitTestObject(drop_6))
{
TweenMax.to(drag_6, 0.5, {x:hit_6X, y:hit_6Y, ease:Cubic.easeOut});
drag_6.mouseEnabled = false;
SoundMixer.stopAll();
drag_6.alpha = 0 ;
hit_6.alpha = 1;
drag_6.buttonMode = false;
}
else
{
TweenMax.to(drag_6, 0.5, {x:back_6X, y:back_6Y, ease:Bounce.easeOut});
SoundMixer.stopAll();
}
}
Make sure the global stage object is accessible.

Sprite movement basically right but

This is basically just drag and double click to set (so drag is temporarily disabled) but the sprite doesn't keep with the mouse- can someone point me to better code- otherwise I'll with go with this- so much more to do.
//The initial event performed when the button is first clicked;
internal var m_nDoubleClickSpeed:Number = 300;
internal var m_toMouse:Number;
internal var moveready:Boolean = false;
internal var initalx:uint;
internal var initialy:uint;
internal var move:Boolean;
internal function clickDoubleClick(e:MouseEvent):void {
if (isSet == false) {
this.startDrag();
}
if (isNaN(m_toMouse)==false) {
clearTimeout(m_toMouse);
HandleDoubleClick();
} else {
m_toMouse = setTimeout(HandleSingleClick, m_nDoubleClickSpeed);
}
}
internal function HandleSingleClick():void {
trace("HandleSingleClick");
trace("isSet");
trace(isSet);
this.stopDrag();
m_toMouse = NaN;
}
internal function HandleDoubleClick():void {
if (isSet == false) {
isSet = true;
this.stopDrag()
} else {
isSet = false;
}
trace("HandleDoubleClick");
m_toMouse = NaN;
}
internal function goodX(inX:Number):Number {
if (inX < 0) {
return 0;
}
if (inX > (stage.stageWidth) ) {
return (stage.stageWidth);
}
return inX;
}
internal function goodY(inY:Number):Number {
if (inY < 0) {
return 0;
}
if (inY > stage.stageHeight) {
return stage.stageHeight;
}
return inY;
}
I'm not sure if I understood you correctly. So you want to start drag on single click and drag until doubleclicked right? If so you can try something like that:
public class ClickAndDrag extends Sprite
{
private var clickTimeout:uint;
private var doubleClickSpeed:int = 500;
private var clickedOnce:Boolean;
private var mouseOnClick:Point;
private var isDragging:Boolean;
public function ClickAndDrag()
{
graphics.beginFill(Math.random()*0xffffff, 1);
graphics.drawCircle(0, 0, 20);
graphics.endFill();
this.buttonMode = true;
addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
}
private function handleMouseDown(e:MouseEvent):void
{
if (isDragging)
{
if (clickedOnce)
handleDoubleClicked();
else
{
//if it's not clicket within next doubleClickSpeed ms then doubleClickSpeed will be set to false;
clickedOnce = true;
clickTimeout = setTimeout( handleClickTimeout, doubleClickSpeed );
}
}
else
{
handleClickAndDrag();
}
}
//clicked once when dragging
private function handleClickOnce():void
{
graphics.clear();
graphics.beginFill(Math.random()*0xffffff, 1);
graphics.drawCircle(0, 0, 20);
graphics.endFill();
}
//clicked once when not dragging
private function handleClickAndDrag():void
{
isDragging = true;
this.addEventListener(Event.ENTER_FRAME, handleFrame);
mouseOnClick = new Point(this.mouseX, this.mouseY);
}
//doubleclicked when dragging
private function handleDoubleClicked():void
{
clearTimeout(clickTimeout);
clickedOnce = false;
this.removeEventListener(Event.ENTER_FRAME, handleFrame);
isDragging = false;
}
private function handleClickTimeout():void
{
clickedOnce = false;
handleClickOnce();
}
private function handleFrame(e:Event):void
{
this.x = stage.mouseX - mouseOnClick.x;
this.y = stage.mouseY - mouseOnClick.y;
}
}
It basically waits for mousedown and if it's already dragging it checks if you clicked once (changes color) ot twice (stops dragging). Or if it's not dragging yet then it starts dragging. You may also want to handle leaving the stage (Event.MOUSE_LEAVE).

Turn this AS3 into an array and work with it

How can I turn this basic function method into an Array and call to easily. Pretty much I am just comparing if objects have become false and then do something...but this just seems like a lot of code for something so easy. any ideas?
var b:Boolean = true;
var i:Boolean = true;
var t:Boolean = true;
var a:Boolean = true;
var m:Boolean = true;
var ii:Boolean = true;
var n:Boolean = true;
var e:Boolean = true;
var s:Boolean = true;
level5_mc.let_b_mc.addEventListener(MouseEvent.CLICK, hitB);
level5_mc.let_i_mc.addEventListener(MouseEvent.CLICK, hitI);
level5_mc.let_t_mc.addEventListener(MouseEvent.CLICK, hitT);
level5_mc.let_a_mc.addEventListener(MouseEvent.CLICK, hitA);
level5_mc.let_m_mc.addEventListener(MouseEvent.CLICK, hitM);
level5_mc.let_ii_mc.addEventListener(MouseEvent.CLICK, hitII);
level5_mc.let_n_mc.addEventListener(MouseEvent.CLICK, hitN);
level5_mc.let_e_mc.addEventListener(MouseEvent.CLICK, hitE);
level5_mc.let_s_mc.addEventListener(MouseEvent.CLICK, hitS);
function hitB(event:MouseEvent){
b=false;
trace("good");
level5_mc.removeChild(level5_mc.let_b_mc);
}
function hitI(event:MouseEvent){
if (b==false){
i=false;
level5_mc.removeChild(level5_mc.let_i_mc);
}
else {
//decrease timer
i=true;
}
}
function hitT(event:MouseEvent){
if (b==false && i==false){
t=false;
level5_mc.removeChild(level5_mc.let_t_mc);
}
else {
//decrease timer
i=true;
}
}
and so forth... edited below
var b=level5_mc.let_b_mc;
var i=level5_mc.let_i_mc;
var t=level5_mc.let_b_mc;
var movieClips:Array = [b,i,t];
var movieClipFlags:Object = {
b:[],
i:[b],
t:[b,i]
};
for each(var mc:MovieClip in movieClips) {
mc.addEventListener(MouseEvent.CLICK,movieClipHit);
}
function movieClipHit(e:MouseEvent) {
var mc:MovieClip = e.target as MovieClip;
if(readyToRemove(mc))
level5_mc.removeChild(mc);
else
trace("Can't remove yet.");
}
function readyToRemove(mc:MovieClip):Boolean {
for each(var mc:MovieClip in movieClipFlags[mc]) {
//if it has parent, it isn't removed yet.
if(mc.parent)
return false;
}
return true;
}
Something like the following should give you some ideas. I haven't tested the code you migt need to make some adjustments depending on your needs.
//put other movies clips in this array too.
var movieClips:Array = [level5_mc.let_b_mc,level5_mc.let_i_mc];
var movieClipFlags:Object = {
level5_mc.let_b_mc:[],
level5_mc.let_i_mc:[level5_mc.let_b_mc],
level5_mc.let_t_mc:[level5_mc.let_b_mc,level5_mc.let_i_mc]
//put other movie clips here too with the movie clips that need to be removed in the array
};
foreach(var mc:MovieClip in movieClips) {
mc.addEventListener(MouseEvent.CLICK,movieClipHit);
}
private function movieClipHit(e:MouseEvent) {
var mc:MovieClip = e.target as MovieClip;
if(readyToRemove(mc))
level5_mc.removeChild(mc);
else
trace("Can't remove yet.");
}
private function readyToRemove(mc:MovieClip):Boolean {
foreach(var mc:MovieClip in movieClipFlags[mc]) {
//if it has parent, it isn't removed yet.
if(mc.parent)
return false;
}
return true;
}
Hope it helps.

how to detect all tween complete in starling-framework

I'm develop a game with starling-framework.my problem is how to detect all tween is complete when I add a set of elements into juggler.
current my plan is
while(some_condition){ //allocate a set of tween
var tween=createTween(tweenCount++);
tween.onComplete=function(){
tweenCount--;
}
}
function checkComplete(){
if(tweenCount==0)
doStuff();
else
setTimeout(checkComplete,1000);
}
and there is any better solution ? thanks for your time!
update
write a simple class to solve this ,seems like better
public class TweenMonitor
{
public function TweenMonitor()
{
throw new Error('static class');
}
private static var refDict:Dictionary = new Dictionary();
public static function monit(tweens:Vector.<Tween>,onAllFinish:Function,id:String='$default$'):void
{
for (var i:int = 0; i < tweens.length; i++)
{
var tween:Tween = tweens[i];
if (tween == null)
continue;
if (refDict[id] == null) {
refDict[id] = 1;
}else
refDict[id]++;
tween.addEventListener(Event.REMOVE_FROM_JUGGLER,function (e:Event):void
{
refDict[id]--;
if (refDict[id] == 0)
onAllFinish && onAllFinish();
});
}
}
}
You simply need the onComplete property of Tween Class (Starling Framework)
Here is an Example :
function addTween() {
var tween:Tween = new Tween(object, 2.0, Transitions.EASE_IN_OUT);
tween.animate("x", object.x + 50);
tween.animate("rotation", deg2rad(45));
tween.fadeTo(0);
tween.onComplete = tween_complete;
Starling.juggler.add(tween);
}
function tween_complete() {
// Handle Tween Complete Action Here
}
EDIT
In case of multiple tweens, you probably could do better by attaching a timer class instead of settimeout:
while(some_condition){ //allocate a set of tween
var tween=createTween(tweenCount++);
tween.onComplete = function() { tweenCount--; }
}
var timer : Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER,update);
timer.start();
function update(e) {
if(tweenCount > 0) return;
timer.stop();
timer = null;
doStuff();
}
I would have done like this:
OnComplete = tweenDone;
...
function tweenDone() {
TweenCount--;
If(TweenCount == 0) doStuff();
}
Wrote this on a cell phone so I had to take a lot of shortcuts, if you need more info, leave a comment!

How to load an external image on a drag & drop object using AS3?

NEW MESSAGE - THE SOLUTION I ENDED UP APPLYING
This is what I used to load an external image into a movie clip and drag and drop.
The feedback part is pretty much the same from the old code.
var holdermc_Arr:Array = new Array(4);
for(var i:uint = 0; i < holdermc_Arr.length; i++)
{
holdermc_Arr[i] = this["panel" + (i+1) + "_mc"];
var myLoader:Loader = new Loader();
var fileRequest:URLRequest = new URLRequest("images/" + (i+1) + ".jpg");
myLoader.load(fileRequest);
holdermc_Arr[i].addChild(myLoader);
holdermc_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
holdermc_Arr[i].addEventListener(MouseEvent.MOUSE_UP, dropIt);
holdermc_Arr[i].buttonMode = true;
}
var startX:Number;
var startY:Number;
var counter:Number = 0;
function pickUp(event:MouseEvent):void {
startX = event.currentTarget.x;
startY = event.currentTarget.y;
setChildIndex(MovieClip(event.currentTarget),numChildren-1);
event.currentTarget.startDrag();
reply_txt.text = "";
}
OLD MESSAGE BELLOW
This is my latest attempt with the current error I am getting.
I actually would like to work with the code of my previous attempt because to me is easier that way to add multiple draggable movie clips.
But whichever the code, what seems to be the fundamental problem is that I am not setting up the property correctly for the "Loader".
Let me know if a link to the example is needed.
The error is the following:
ReferenceError: Error #1069: Property dropTarget not found on
flash.display.Loader and there is no default value. at
cs5test_fla::MainTimeline/ReleaseToDrop()
The code is the following
var myLoader:Loader = new Loader();
panel1_mc.addChild(myLoader);
var url:URLRequest = new URLRequest("uploadedImages/photo_1.jpeg");
myLoader.load(url);
var startX:Number;
var startY:Number;
var counter:Number = 0;
panel1_mc.addEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);
function ClickToDrag(event:MouseEvent):void
{
panel1_mc.startDrag();
startX = event.target.x;
startY = event.target.y;
reply_txt.text = "";
event.target.parent.addChild(event.target);
}
stage.addEventListener(MouseEvent.MOUSE_UP, ReleaseToDrop);
function ReleaseToDrop(event:MouseEvent):void
{
panel1_mc.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
reply_txt.text = "Good Job!";
event.target.x = myTarget.x;
event.target.y = myTarget.y;
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);
event.target.removeEventListener(MouseEvent.MOUSE_UP, ReleaseToDrop);
event.target.buttonMode = false;
counter++;
} else {
reply_txt.text = "Try Again!";
event.target.x = startX;
event.target.y = startY;
}
if(counter == 1){
reply_txt.text = "Congrats, you're finished!";
}
}
OLDER MESSAGE BELLOW
I am still struggling.
I am new to all this so I am really tying the best I can to grasp it.
The ActionScript I am trying to work with is all the way at the bottom.
I'd really appreciate if someone tries to look at this code and tell me how can I load external images to each draggable movie clip with out getting the following error.
ReferenceError: Error #1069: Property startDrag not found on
flash.display.Loader and there is no default value. at
dragdropDilema_fla::MainTimeline/pickUp() ReferenceError: Error #1069:
Property stopDrag not found on flash.display.Loader and there is no
default value. at dragdropDilema_fla::MainTimeline/dropIt()
I tried to use a simple var loader but for what I can see on the error, I have to set up the startDrag property to work with the Loaded image and not with the draggable movie clips that are currently in place??
I thought I would have just been simple if I had use the following for each:
var myLoader:Loader = new Loader();
imageHolder.addChild(myLoader);
var url:URLRequest = new URLRequest("uploadedImages/photo_1.jpeg");
myLoader.load(url);
and then do something like:
square_mc.imageHolder.addChild(myLoader);
but when I do that I get the error anyway when I click on the image.
Here is the entire code: It is supposed to count when the object meet their target and gives a message all through out and at the end.
panel1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
panel1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
panel1_mc.buttonMode = true;
var startX:Number;
var startY:Number;
var counter:Number = 0;
function pickUp(event:MouseEvent):void {
startX = event.target.x;
startY = event.target.y;
event.target.startDrag(true);
reply_txt.text = "";
event.target.parent.addChild(event.target);
}
function dropIt(event:MouseEvent):void {
event.target.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
reply_txt.text = "Good Job!";
event.target.x = myTarget.x;
event.target.y = myTarget.y;
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
event.target.buttonMode = false;
counter++;
} else {
reply_txt.text = "Try Again!";
event.target.x = startX;
event.target.y = startY;
}
if(counter == 1){
reply_txt.text = "Congrats, you're finished!";
}
}
Thanks.
import flash.display.DisplayObjectContainer;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
var _dragMc:MovieClip = new MovieClip();
addChild(_dragMc);
loadImage("image.jpg")
function loadImage(path:String):void
{
var _loader:Loader = new Loader();
var _loaderToLoad:URLRequest = new URLRequest(path);
_loader.load(_loaderToLoad);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded, false, 0, true);
}
function imageLoaded(evt:Event):void
{
addLoader(evt.currentTarget.loader, _dragMc);
}
function addLoader(loader:Loader, container:DisplayObjectContainer):void
{
container.addChild(loader);
addListeners();
}
function addListeners():void
{
_dragMc.addEventListener(MouseEvent.MOUSE_DOWN, setDrag, false, 0, true);
}
function setDrag(evt:MouseEvent):void
{
_dragMc.addEventListener(MouseEvent.MOUSE_MOVE, doDrag, false, 0, true);
_dragMc.removeEventListener(MouseEvent.MOUSE_DOWN, setDrag);
}
function doDrag(evt:MouseEvent):void
{
_dragMc.startDrag(false, null);
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag, false, 0, true);
_dragMc.removeEventListener(MouseEvent.MOUSE_MOVE, doDrag);
}
function endDrag(evt:MouseEvent):void
{
_dragMc.stopDrag();
_dragMc.addEventListener(MouseEvent.MOUSE_DOWN, setDrag, false, 0, true);
stage.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
}
This loads an image onto a MovieClip called _dragMc. The rest from there is up to you. I also added some quick code that will enable you to drag _dragMc around.