Welcome to Dream.In.Code
Getting Help is Easy!

Join 118,309 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,714 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Need help in converting to As3

 
Reply to this topicStart new topic

Need help in converting to As3

Jonson
post 27 Jul, 2008 - 08:35 PM
Post #1


New D.I.C Head

*
Joined: 17 Apr, 2008
Posts: 40

Hello, i need help in converting this code to as3.. especially this line i want to understand what it means:
circle_mc.onRelease = circle_mc.onReleaseOutside=function ()
What it mean by the =? How to create function inside a function in As3?

I tried converting it to as3 but have some bug.


THE AS2 CODE:
CODE
circle_mc.onPress = function() {
startDrag(this);
};
circle_mc.onRelease = circle_mc.onReleaseOutside=function () {
stopDrag();
if (this._droptarget == "/targetCircle") {
this.onTarget = true;
_root.targetCircle.gotoAndStop(2);
} else {
this.onTarget = false;
_root.targetCircle.gotoAndStop(1);
}
};
//the variables below will store the clips starting position
circle_mc.myHomeX=circle_mc._x;
circle_mc.myHomeY=circle_mc._y;
//the variables below will store the clips end position
circle_mc.myFinalX = 443;
circle_mc.myFinalY = 294;
circle_mc.onMouseDown = function() {
//this variable tells us if the mouse is up or down
mousePressed = true;
};
circle_mc.onMouseUp = function() {
mousePressed = false;
};
circle_mc.onEnterFrame = function() {
//all these actions basically just say "if the mouse is up (in other words - the clip is not being dragged)
// then move the MC back to its original starting point (with a smooth motion)"
if (mousePressed == false && this.onTarget == false) {
this._x -= (this._x-this.myHomeX)/5;
this._y -= (this._y-this.myHomeY)/5;
//if the circle is dropped on any part of the target it slides to the center of the target
} else if (mousePressed == false && this.onTarget == true) {
this._x -= (this._x-this.myFinalX)/5;
this._y -= (this._y-this.myFinalY)/5;
}
};




AS3 CODE(I tried to convert but some bug)

[code]var onTarget:Boolean;
var mousePressed:Boolean;
var myHomeX:Number;
var myHomeY:Number;
var myFinalX:Number;
var myFinalY:Number;

circle_mc.addEventListener("mouseDown", mouseDownHandler);
circle_mc.addEventListener("mouseDown", mouseDownHandler2);
circle_mc.addEventListener("mouseUp", mouseUpHandler);
circle_mc.addEventListener("mouseUp", mouseUpHandler2);
circle_mc.addEventListener("enterFrame", enterFrameHandler);

function mouseDownHandler(event:Event):void
{
startDrag();
}

function mouseUpHandler(event:Event):void
{
stopDrag();
if (this.dropTarget == "targetCircle")
{
this.onTarget = true;
_root.targetCircle.gotoAndStop(2);
}
else
{
this.onTarget = false;
_root.targetCircle.gotoAndStop(1);
}
}

myHomeX = circle_mc.x;
myHomeY = circle_mc.y;

myFinalX = 443;
myFinalY = 294;


function mouseDownHandler2(event:Event):void
{
mousePressed = true;
}

function mouseUpHandler2(event:Event):void
{
mousePressed = false;
}

function enterFrameHandler(event:Event):void
{
if(mousePressed == false && this.onTarget == false)
{
this.x -= (this.x-this.myHomeX)/5;
this.y -= (this.y-this.myHomeY)/5;
}
else if(mousePressed == false && this.onTarget == true)
{
this.x -= (this.x-this.myFinalX)/5;
this.y -= (this.y-this.myFinalY)/5;
}
}[\code]

This post has been edited by Jonson: 27 Jul, 2008 - 08:36 PM
User is offlineProfile CardPM

Go to the top of the page


theRemix
post 27 Jul, 2008 - 08:59 PM
Post #2


D.I.C Regular

***
Joined: 19 Oct, 2005
Posts: 364



Thanked 1 times
My Contributions


[quote name='Jonson' date='27 Jul, 2008 - 08:35 PM' post='390805']
Hello, i need help in converting this code to as3.. especially this line i want to understand what it means:
circle_mc.onRelease = circle_mc.onReleaseOutside=function ()
What it mean by the =? How to create function inside a function in As3?

I tried converting it to as3 but have some bug.


THE AS2 CODE:
CODE
circle_mc.onPress = function() {
startDrag(this);
};
circle_mc.onRelease = circle_mc.onReleaseOutside=function () {
stopDrag();
if (this._droptarget == "/targetCircle") {
this.onTarget = true;
_root.targetCircle.gotoAndStop(2);
} else {
this.onTarget = false;
_root.targetCircle.gotoAndStop(1);
}
};
//the variables below will store the clips starting position
circle_mc.myHomeX=circle_mc._x;
circle_mc.myHomeY=circle_mc._y;
//the variables below will store the clips end position
circle_mc.myFinalX = 443;
circle_mc.myFinalY = 294;
circle_mc.onMouseDown = function() {
//this variable tells us if the mouse is up or down
mousePressed = true;
};
circle_mc.onMouseUp = function() {
mousePressed = false;
};
circle_mc.onEnterFrame = function() {
//all these actions basically just say "if the mouse is up (in other words - the clip is not being dragged)
// then move the MC back to its original starting point (with a smooth motion)"
if (mousePressed == false && this.onTarget == false) {
this._x -= (this._x-this.myHomeX)/5;
this._y -= (this._y-this.myHomeY)/5;
//if the circle is dropped on any part of the target it slides to the center of the target
} else if (mousePressed == false && this.onTarget == true) {
this._x -= (this._x-this.myFinalX)/5;
this._y -= (this._y-this.myFinalY)/5;
}
};




AS3 CODE(I tried to convert but some bug)

CODE
var onTarget:Boolean;
var mousePressed:Boolean;
var myHomeX:Number;
var myHomeY:Number;
var myFinalX:Number;
var myFinalY:Number;

circle_mc.addEventListener("mouseDown", mouseDownHandler);
circle_mc.addEventListener("mouseDown", mouseDownHandler2);
circle_mc.addEventListener("mouseUp", mouseUpHandler);
circle_mc.addEventListener("mouseUp", mouseUpHandler2);
circle_mc.addEventListener("enterFrame", enterFrameHandler);

function mouseDownHandler(event:Event):void
{
    startDrag();
}

function mouseUpHandler(event:Event):void
{
    stopDrag();
    if (this.dropTarget == "targetCircle")
    {
        this.onTarget = true;
        _root.targetCircle.gotoAndStop(2);
    }
    else
    {
        this.onTarget = false;
        _root.targetCircle.gotoAndStop(1);
    }
}

myHomeX = circle_mc.x;
myHomeY = circle_mc.y;

myFinalX = 443;
myFinalY = 294;


function mouseDownHandler2(event:Event):void
{
    mousePressed = true;
}

function mouseUpHandler2(event:Event):void
{
    mousePressed = false;
}

function enterFrameHandler(event:Event):void
{
    if(mousePressed == false && this.onTarget == false)
    {
        this.x -= (this.x-this.myHomeX)/5;
        this.y -= (this.y-this.myHomeY)/5;
    }
    else if(mousePressed == false && this.onTarget == true)
    {
        this.x -= (this.x-this.myFinalX)/5;
        this.y -= (this.y-this.myFinalY)/5;
    }
}[\code]
[/quote]


it means you are overwriting the functions of the circle_mc object, using the same code block.

circle_mc.onRelease = circle_mc.onReleaseOutside=function ()  

in AS3, MovieClip objects do not have onRelease and onReleaseOutside methods.

instead, you would add listeners to the circle_mc object.

(first make sure you import the packages you need in your class file)
[code]import flash.display.MovieClip
import flash.events.Event
import flash.events.MouseEvent


now somewhere, maybe in your constructor, assuming that circle_mc is on the stage and is a child display object of this class
CODE
circle_mc.addEventListener(MouseEvent.CLICK, circle_mc_clicked);


now define that method
CODE
private function circle_mc_clicked(event:MouseEvent):void{
// stuff happens when circle_mc is clicked, here
}



hth
User is offlineProfile CardPM

Go to the top of the page

Jonson
post 28 Jul, 2008 - 07:02 PM
Post #3


New D.I.C Head

*
Joined: 17 Apr, 2008
Posts: 40

ya i know that as3 dun have onpress which have become listener, but i want to know why this code circle_mc.onRelease = circle_mc.onReleaseOutside=function() works in as2? i thought theres only circle_mc.onRelease = function? instead of code circle_mc.onRelease =
circle_mc.onReleaseOutside=function()
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 28 Jul, 2008 - 07:20 PM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,635



Thanked 63 times

Dream Kudos: 1075
My Contributions


code circle_mc.onRelease = circle_mc.onReleaseOutside = function() works in AS 2 because it is declaring both the events as the smae function, though knowing AS it is probably declaring circle_mc.onReleaseOutside = function and then copying the function for onRelease. I am not sure exactly what you are asking, it works because of the way the AS compiler does the compiling... It is basically the same as declaring each of the independant events as the same function.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/10/08 11:51AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month