3 Replies - 1664 Views - Last Post: 28 July 2008 - 07:20 PM

#1 Jonson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 17-April 08

Need help in converting to As3

Posted 27 July 2008 - 08:35 PM

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:
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 July 2008 - 08:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help in converting to As3

#2 theRemix  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 440
  • Joined: 19-October 05

Re: Need help in converting to As3

Posted 27 July 2008 - 08:59 PM

[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:
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)

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
circle_mc.addEventListener(MouseEvent.CLICK, circle_mc_clicked);


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



hth
Was This Post Helpful? 0
  • +
  • -

#3 Jonson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 17-April 08

Re: Need help in converting to As3

Posted 28 July 2008 - 07:02 PM

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()
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,213
  • Joined: 07-September 06

Re: Need help in converting to As3

Posted 28 July 2008 - 07:20 PM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1