Drag stuffs, Actionscript 2.0 to 3.0 - actionscript-3

I have done a drag and match flash stuff by using Actionscipt2.0, but now I need to change it to 3.0. After I changed it, the flash can no longer drag the stuffs, can anyone please take a look at the code to see which part I was converting it wrong. Thanks.
Actionscript 2.0 version:
stop();
var randomPositionFrame = int(Math.random()*9)+1;
content_mc.gotoAndStop(randomPositionFrame);
for(var i=1; i<=5; i++){
eval("content_mc.matching_term_"+i)._alpha = 0;
eval("content_mc.matching_term_"+i).onPress = function(){
if(this._currentframe == 1){
this.startDrag();
}
}
eval("content_mc.matching_term_"+i).onRelease = onMouseUp = function(){
this.stopDrag();
}
eval("content_mc.matching_desc_"+i)._alpha = 0;
eval("content_mc.matching_desc_"+i).onPress = function(){
if(this._currentframe == 1){
this.startDrag();
}
}
eval("content_mc.matching_desc_"+i).onRelease = onMouseUp = function(){
this.stopDrag();
}
}
Actionscript 3.0 version:
stop();
var randomPositionFrame = int(Math.random()*9)+1;
content_mc.gotoAndStop(randomPositionFrame);
for(var i=1; i<=5; i++){
this["content_mc.matching_term_"+i]._alpha = 0;
this["content_mc.matching_term_"+i].addEventListener(MouseEvent.MOUSE_DOWN,function():void {
if(this._currentframe == 1){
this.startDrag();
}
}
);
this["content_mc.matching_term_"+i].addEventListener(MouseEvent.MOUSE_UP,function():void {
stage.addEventListener(MouseEvent.MOUSE_UP, doMouseUp, false, 0, true);
function doMouseUp($evt:MouseEvent):void
{
this.stopDrag();
}
}
);
this["content_mc.matching_desc_"+i]._alpha = 0;
this["content_mc.matching_term_"+i].addEventListener(MouseEvent.MOUSE_DOWN,function():void {
if(this._currentframe == 1){
this.startDrag();
}
}
);
this["content_mc.matching_desc_"+i].addEventListener(MouseEvent.MOUSE_UP,function():void {
this.stopDrag();
}
);
}

In your Mouse Listener you have to get the target/object from the event. Meaning the target you applied your listener to.
Create two listener functions and apply them to your addEventListener call:
function mouseDownHandler(e:MouseEvent):void
{
var object = e.target;
object.startDrag();
}
function mouseUpHandler(e:MouseEvent):void
{
var obj = e.target;
obj.stopDrag();
}
Use addEventListener like this:
yourObj.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
yourObj.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

This won't work
this["content_mc.matching_term_"+i]
You need to talk to each object separately. So first this then content_mc.
this.content_mc["matching_term_"+i]
Might help a little.

Related

swap depthes when movie clips loaded from library

I have two movie clips in the library with linkage.
on the stage I have two buttons - each load a movie clip to a specific mc target on the stage.
I also have a third button that removes the mc target, to clear the stage.
I want to know how can I change the code in AS3 so the loaded movie clips will not show at the same time, but swap each other, like I used to use depth in AS2.
This is the code:
var myIgool = new igool ();
var myRibooa = new ribooa ();
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
mc_all.addChild (myIgool);
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
mc_all.addChild (myRibooa);
}
unloadall.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_6);
function fl_MouseClickHandler_6(event:MouseEvent):void
{
removeChild(mc_all);
;
}
I would recommend something like this:
var myIgool = new igool ();
var myRibooa = new ribooa ();
mc_all.addChild(myIgool);
mc_all.addChild(myRibooa);
myIgool.visible = false;
myRibooa.visible = false;
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
myIgool.visible = true;
myRibooa.visible = false;
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
myIgool.visible = false;
myRibooa.visible = true;
}
unloadall.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_6);
function fl_MouseClickHandler_6(event:MouseEvent):void
{
myIgool.visible = false;
myRibooa.visible = false;
}
But if you really want to swap, you could also do the following, however I recommend setting the visible flag as it's more efficient rather than covering something up that wouldn't need to draw.
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
if (myIgool.parent != mc_all)
{
mc_all.addChild(myIgool);
}
else
{
mc_all.setChildIndex(myIgool, mc_all.numChildren - 1);
}
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
if (myRibooa.parent != mc_all)
{
mc_all.addChild(myRibooa);
}
else
{
mc_all.setChildIndex(myRibooa, mc_all.numChildren - 1);
}
}

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!

Flash AS3 Press and Hold Button

I'm no expert in Flash but I found a way in AS2 to make a "press and hold" button. Now I'm working with AS3 and I'd like this code to be converted to AS3. Can someone help ?
stop();
function startTimer(mc, conversionTime) {
mc.onEnterFrame = function() {
if ((getTimer() / 1000) - conversionTime > 1) {
delete this.onEnterFrame;
gotoAndStop(3);
}
};
}
button1.onPress = function() {
var conversionTime:Number = getTimer() / 1000;
startTimer(this, conversionTime);
this.onRelease = function() {
if (this.onEnterFrame != null) {
gotoAndStop(2);
}
delete this.onEnterFrame;
};
};
Thanks !
In AS3 it would look like this:
mc.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
var myTimer:Timer = new Timer(5000,1);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, _buttonPressedEnoughLong);
private function _mouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
myTimer.start();
}
private function _mouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, _mouseUp);
myTimer.reset();
}
private function _buttonPressedEnoughLong(e:TimerEvent) : void {
e.currentTarget.reset();
// Do stuff
}
You need to hold button 5 seconds, before event will fire.
Change mc.onEnterFrame = function() ... to:
mc.addEventListener(Event.ENTER_FRAME, onEvent);
function onEvent(e:Event)
{
if ((getTimer() / 1000) - conversionTime > 1)
{
this.removeEventListener(Event.ENTER_FRAME, onEvent);
gotoAndStop(3);
}
}
Change button1.onPress = function() ... to:
button1.addEventListener(MouseEvent.MOUSE_DOWN, onBtnDown);
function onBtnDown(e:MouseEvent)
{
var conversionTime:Number = getTimer() / 1000;
startTimer(this, conversionTime);
function onBtnUp(e:MouseEvent)
{
if (this.hasEventListener(Event.ENTER_FRAME))
{
gotoAndStop(2);
this.removeEventListener(Event.ENTER_FRAME, onEvent);
}
}
}

to create quiz using StartDrag and stopDrag

Actually i have a quiz.fla. In the file ,two of them fill inthe blank questions and others are
multiple questions.
square1_mc must run only once not twice. İf user correct selected, doesnt run it again.
However,if mybadscoretext is 1 not increase 2,3,4. :S
how i can do all?
stop();
var myScore:Number = 0;
var myBadScore:Number=0;
square1_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(e:MouseEvent):void
{
e.target.startDrag();
}
function drop(e:MouseEvent):void
{
square1_mc.stopDrag();
if (square1_mc.hitTestObject(square2_mc)== true)
{
square1_mc.x=129;
square1_mc.y=133;
this.graphics.clear();
this.graphics.lineStyle(2, 000000);
this.graphics.moveTo(square1_mc.x, square1_mc.y);
this.graphics.lineTo(square2_mc.x, square2_mc.y);
this.graphics.endFill();
myScore += 1;
score_txt.text=String(myScore);
square1_mc.removeEventListener(MouseEvent.MOUSE_DOWN, drag);
}
else
{
square1_mc.x=129;
square1_mc.y=133;
myBadScore += 1;
mybadscore_txt.text=String(myBadScore);
}
}
Add a variable to keep track of whether the bad score has been added:
var badMarked:Boolean = false;
function drag(e:MouseEvent):void
{
e.target.startDrag();
badMarked = false;
}
[...]
function drop(e:MouseEvent):void
{
if (square1_mc.hitTestObject(square2_mc)== true)
{
[...]
}
else if ( !badMarked )
{
square1_mc.x=129;
square1_mc.y=133;
myBadScore += 1;
mybadscore_txt.text=String(myBadScore);
badMarked = true;
}
}

Actionscript 3.0 Keyboard event listener error

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.