Convert AS2 Code to AS3 Format - actionscript-3

please help me out converting the code below into as3. I have no ideea what is "SHEMA" , "BASE", "GLANCE" etc for AS3. THanks a lot.
This code is being written inside a movie clip.
function CopyFromShema(sframe)
{
SHEMA.gotoAndStop(sframe);
GLANCE.filters = SHEMA.GLANCE.filters;
BASE.filters = SHEMA.BASE.filters;
CAPTION.filters = SHEMA.CAPTION.filters;
}
SHEMA._visible = false;
SHEMA.gotoAndStop(1);
BASE.scale9Grid = new flash.geom.Rectangle(10, 10, 100, 5);
GLANCE.scale9Grid = new flash.geom.Rectangle(10, 6, 100, 2);
onRollOver = function ()
{
CopyFromShema(3);
}
;
onRollOut = function ()
{
CopyFromShema(2);
}
;
onPress = function ()
{
CopyFromShema(4);
}
;
onRelease = function ()
{
CopyFromShema(3);
}
;
onDragOver = function ()
{
onPress();
}
;
onDragOut = function ()
{
onRollOut();
}
;

as pointed in line "3" of provided code in question, SHEMA.gotoAndStop(sframe);
they are all MovieClips and not As2 Classes/Keywords.
everything is ok in AS3 just replace (do it for all event fucntions)
onRollOver = function ()
{
CopyFromShema(3);
};
with As3 Event handlers
stage.addEventListener(MouseEvent.ROLL_OVER, function(e:MouseEvent):void {
CopyFromShema(3);
});
about other MouseEvents :
onRollOut : MouseEvent.ROLL_OUT
onPress : MouseEvent.MOUSE_DOWN
onRelease : MouseEvent.MOUSE_UP
onDragOver : N/A (its MOUSE_OVER while MOUSE_DOWN)
onDragOut : N/A (its MOUSE_OUT while MOUSE_DOWN)
so what to do with non available events in as3? they are available but you have to handling them with bunch of available events, here is an example of it.
[update] why chaning AS2 to AS3?
most times, there is no real need for porting AS2 to AS3, but you can simply compile AS2 projects as swf, then embedding them in to the AS3 projects, and let them communicate with each other trough a LocalConnection as mentioned here

Related

unable to remove movieclip itself from stage as3

I had a button, with that button a movieclip is called on the stage and it is working fine with the below script.
I just updated the script and it is now working fine with the removing of movieclip from the stage, but now the issue came with playing the movieclip. Now the movieclip is not playing well.
var btn: btn_Lemon = new btn_Lemon();
var mc: mc_lemon = new mc_lemon();
addChild(btn);
btn.x = 304.45;
btn.y = 209.8;
btn.addEventListener(MouseEvent.MOUSE_DOWN, login, false, 0, true);
function login(event: MouseEvent): void {
stage.addChild(mc);
mc.x = 304.45;
mc.y = 209.8;
mc.addEventListener(Event.ENTER_FRAME, fadeOver, false, 0, true);
}
function fadeOver(event: Event): void {
if (event.currentTarget.currentFrame == 25) {
event.currentTarget.removeEventListener(Event.ENTER_FRAME, fadeOver);
stage.removeChild(mc);
}
}
What is going on please help somebody
Presuming it's a timeline you're trying to play inside lemon_mc and it's the play method that doesn't seem to run properly.
In my experience Flash can sometimes be a bit fickle with playing as I recall.
Here are some pointers for pushing the MC to play:
Force the a mc to play at the end of the
function login(event: MouseEvent): void {
stage.addChild(mc);
mc.x = 304.45;
mc.y = 209.8;
mc.addEventListener(Event.ENTER_FRAME, fadeOver, false, 0, true);
mc.play();
}
Simple add
play();
at the first frame inside the movie clip you want to play.
Also if the playback is a bit fickle at certain scenarios you can define with code, then you can use a test:
if (mc.isPlaying == false) {
mc.play();
}
As a beginner I found the as3 documentation from adobe to be easy to understand and apply. You might find some other properties to read or methods you can manipulate to force the play method:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/MovieClip.html

How to reset Accelerometer Event As3

In my project, at the start of game accelerometer event works fine. While game reaches the game over page and click on restart button. All the objects are working good when restarting the whole game also all values have been reset but accelerometer is not working.
Thanks in Advance.
The code follows:
if (Accelerometer.isSupported)
{
acc = new Accelerometer();
acc.addEventListener(AccelerometerEvent.UPDATE,updateFn);
}
public function updateFn(e:AccelerometerEvent):void
{
targetX = e.accelerationX * 9.8;
}
Just register the Accelerator once the app was launched/activated and save it's value to global variables on each accelerator update and disabled it each time the app is put to the background / deactivated / exit. Eg:
NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleApplicationDeactivated);
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleApplicationActivated);
function handleApplicationActivated( e:Event):void {
// Check if Accelerometer is already activated
if( acc != null ) return;
acc = new Accelerometer();
acc.addEventListener(AccelerometerEvent.UPDATE,update);
}
function update( e:AccelerometerEvent ):void {
GlobalVars.accX = e.accelerationX;
GlobalVars.accY = e.accelerationY;
GlobalVars.accZ = e.accelerationZ;
}
function handleApplicationDeactivated( e:Event):void {
acc.removeEventListener(AccelerometerEvent.UPDATE,update);
acc = null;
}
edit: you might will want to use this activate/deactivate code instead : NativeApplication DEACTIVATE-ACTIVATE app when in the background since the NativeApplication have some issues.

Dragging movie clips in Action Script 3

Hi so recently I have been attempting to Drag a movie clip in AS3 but I'm having some trouble picking up with the hit tests anyone got any ideas? Just to clarify, the issue is that when the movieclips hit the drag test object, they're not executing the gotoframe() function.
initDrag() adds action listeners:
MOUSE_DOWN on the object
MOUSE_UP on the stage so it doesn’t matter if you are off the object
endDrag() removes the action listeners; call this (for each object) before you go to another frame
startADrag()create a rectangle within which the object can be dragged (in this case the stage)
call startDrag() on the object
stopADrag() call stopDrag() on the object from currentObject (but only if currentObject is not null).
var currentObject:MovieClip = null;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);
function initDrag(obj:MovieClip )
{
obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function startADrag(e:MouseEvent):void
{
currentObject = (MovieClip)(e.target);
var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
currentObject.startDrag(false,rect);
}
function stopADrag(e:MouseEvent):void
{
if (currentObject != null)
{
currentObject.stopDrag();
}
}
if(block1.hitTestObject(dragtest)){
gotoAndStop("lose");
}
if(block2.hitTestObject(dragtest)){
gotoAndStop(27);
}
if(block3.hitTestObject( dragtest)){
gotoAndStop("lose");
}
if(block4.hitTestObject( dragtest)){
gotoAndStop("lose");
}
thanks for any advice or answers.
The following code should work as expected. The problem is, as i already stated in my comment, that your calls to hitTestObject(obj) only get executed once, at the very beginning of your application. What you need to do though is to check it constantly.
Think about it, if your calls to hitTestObject-calls only get executed once at the beginning, when you didn't even have a chance to drag one of your objects, it will always return false, right? Because your objects are still in their initial position (outside of the dragtest objecti must assume).
With an event listener for Event.ENTER_FRAME you check it once per frame instead. So even if all the results for hitTestObject are false, it will check them all again on the next frame (if you are currently dragging, controlled through a simple boolean called dragging).
var currentObject:MovieClip = null;
var dragging:Boolean = false;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);
addEventListener(Event.ENTER_FRAME, checkForHit);
function checkForHit(e:Event):void{
if(dragging){
if(block1.hitTestObject(dragtest)){
gotoAndStop("lose");
}
if(block2.hitTestObject(dragtest)){
gotoAndStop(27);
}
if(block3.hitTestObject( dragtest)){
gotoAndStop("lose");
}
if(block4.hitTestObject( dragtest)){
gotoAndStop("lose");
}
}
}
function initDrag(obj:MovieClip )
{
obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function startADrag(e:MouseEvent):void
{
currentObject = (MovieClip)(e.target);
var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
currentObject.startDrag(false,rect);
dragging = true;
}
function stopADrag(e:MouseEvent):void
{
if (currentObject != null)
{
currentObject.stopDrag();
dragging = false;
}
}

want to change the code actionscript2 to actionscript3?

i am newbie to flash.i need to change the below actionscript code to actionscript 3.0 code.
i am currently working on drag and drop. so i want to duplicate the movieclip while dragging i found the code on internet but it is actionscript 2.0 so please convert it to as3. the box is a instance name of a movieclip.
the code blocks are:
var num:Number = 0
box.onPress = function(){
num++
duplicateMovieClip(box ,"box"+num, _root.getNextHighestDepth())
_root["box"+num].startDrag();
}
box.onReleaseOutside = function(){
trace(_root["box"+num])
stopDrag();
}
If you dont want to use seperate .as file, follow this steps:
1- assign AS linkage to box movieClip (in library panel):
2- Select frame 1 on the timeline, and paste this code in the Actions panel:
var boxes:Array=[];
//var box:Box=new Box();
//addChild(box);
box.addEventListener(MouseEvent.MOUSE_DOWN,generateBox);
function generateBox(e:MouseEvent):void{
var newBox:Box=new Box();
newBox.x = e.target.x;
newBox.y = e.target.y;
newBox.startDrag();
newBox.addEventListener(MouseEvent.MOUSE_UP,stopD);
newBox.addEventListener(MouseEvent.MOUSE_DOWN,startD);
boxes.push(newBox);
addChild(newBox);
}
function startD(e:MouseEvent):void{
e.target.startDrag();
}
function stopD(e:MouseEvent):void{
e.target.stopDrag();
}
Unfortunately, there's no duplicateMovieClip analog in AS3, so you'll have to create a Class for your box movieClip template. Let's say it will be called BoxTemplate. (You can google how to create Classes for your library object). Add a Class with this name and add this code (event subscription in the constructor and a private event listener). You'll get something like this:
package
{
public class BoxTemplate
{
public function BoxTemplate()
{
addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
}
private function onMouseUp(e:MouseEvent):void
{
stopDrag();
}
}
Leave your present instance of this symbol on the stage. This is your code in the frame:
import flash.event.MouseEvent
box.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
var newBox:BoxTemplate = new BoxTemplate();
newBox.x = e.target.x;
newBox.y = e.target.y;
addChild(newBox);
newBox.startDrag();
}
It will allow you to infinitely clone your boxes. Of course, you can add all of them in the array to keep the references.

translating AS3 dispatchEvent into AS2

I have one as3 file which handles other files (small games). All files are AS3 except for one which is AS2 and it is too big for me to rewrite it into AS3.
In AS3 i use:
dispatchEvent(new Event("ending", true));
when game is finished.
I need to dispatch "ending" in AS2 file, so my AS3 main file can do it's magic.
Could someone translate this line from as3 to as2?
The event model is just a bunch of callbacks associated with event types (strings). The EventDispatcher maintains this association and iterates over the callbacks when a specific event is triggered by it.
This is pretty trivial to recreate yourself, and in you case you can simplify it greatly.
Here's an example of what could be a simple EventDispatcher in AS2:
function EventDispatcher():Object
{
var listeners:Object = { };
return {
addEventListener: function(type:String, callback:Function)
{
listeners[type] = callback;
},
dispatchEvent: function(type:String)
{
for(var i:String in listeners)
{
if(i === type) listeners[i]();
}
}
};
}
And its implementation:
// Create the event dispatcher.
var eventDispatcher:Object = EventDispatcher();
// Add an event listener as with AS3.
eventDispatcher.addEventListener("ending", endGame);
// Define the handler function.
function endGame():Void
{
trace("Game has ended.");
}
// Dispatch an event.
eventDispatcher.dispatchEvent("ending");
If you want to bring it closer to the AS3 Event model, you need to create an 'Event' object within the dispatchEvent loop and pass that to the handler, something like this:
dispatchEvent: function(type)
{
for(var i:String in listeners)
{
var event:Object = { type: i, target: this };
if(i === type) listeners[i](event);
}
}