As3 Pinch Zoom picture from point of gesture keeping ratio - actionscript-3

I am using below code to Zoom In/Out + Panning from point of gesture, works great for X/Y zoom gestures together, but it lost ratio scale if you do only X or only Y.
It is just a single MC "largeImageContainer", easy to duplicate the issue on Touch and Gesture over animate.
Any help will be really appreciated ;)
import flash.events.TransformGestureEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import fl.motion.MatrixTransformer;
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);
function onZoom(event: TransformGestureEvent): void {
//trace(e.localX);
var locX: Number = event.localX;
var locY: Number = event.localY;
var stX: Number = event.stageX;
var stY: Number = event.stageY;
var prevScaleX: Number = largeImageContainer.scaleX;
var prevScaleY: Number = largeImageContainer.scaleY;
var mat: Matrix;
var externalPoint = new Point(stX, stY);
var internalPoint = new Point(locX, locY);
largeImageContainer.scaleX *= event.scaleX;
largeImageContainer.scaleY *= event.scaleY;
if (event.scaleX > 1 && largeImageContainer.scaleX > 6) {
largeImageContainer.scaleX = prevScaleX;
largeImageContainer.scaleY = prevScaleY;
}
if (event.scaleY > 1 && largeImageContainer.scaleY > 6) {
largeImageContainer.scaleX = prevScaleX;
largeImageContainer.scaleY = prevScaleY;
}
if (event.scaleX < 1.1 && largeImageContainer.scaleX < 1) {
largeImageContainer.scaleX = prevScaleX;
largeImageContainer.scaleY = prevScaleY;
}
if (event.scaleY < 1.1 && largeImageContainer.scaleY < 1) {
largeImageContainer.scaleX = prevScaleX;
largeImageContainer.scaleY = prevScaleY;
}
mat = largeImageContainer.transform.matrix.clone();
MatrixTransformer.matchInternalPointWithExternal(mat, internalPoint, externalPoint);
largeImageContainer.transform.matrix = mat;
}
largeImageContainer.addEventListener(MouseEvent.MOUSE_DOWN, f_begin);
largeImageContainer.addEventListener(MouseEvent.MOUSE_UP, f_end);
function f_begin(e: MouseEvent): void {
largeImageContainer.startDrag();
}
function f_end(e: MouseEvent): void {
largeImageContainer.stopDrag();
}

Actually this did it what i want ;) keeping the ratio since i am focusing on Y more than X.
largeImageContainer.scaleX *= event.scaleY;
largeImageContainer.scaleY *= event.scaleY;

Related

Is anyone interested in looking at my code for a game? I'm getting strange errors

I'm trying to make my first real, simple shooter game. I've read through tutorials online and instead of directly copying code, I tried to take things the next level to actually understand the code and add my own twists. With this in mind, I've recently been learning arrays and not sure if I am using them correctly.
I'm getting strange, intermittent
Error #2025 DisplayObject must be a child of the caller
messages in the output window, not the compiler window.
So, I don't know what line of code is generating this problem. By commenting out blocks of code I have narrowed it down to the modules labeled "CLEANUP MISSED ENEMIES" and "BULLET RATE OF SHOOTING", but the 'why' is beyond my understanding.
I am sure there will be many great comments pointing out conventions I get wrong and errors I am making. I value every chance to learn so please give your input as you see fit! I bet there are way better methods to do the things I am doing!
package{
import flash.events.Event;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import com.greensock.*;
import com.greensock.easing.*;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.geom.ColorTransform;
import fl.motion.Color;
import flash.display.DisplayObject;
public class chopper extends MovieClip{
public function chopper(){
boot();
}
public function boot():void{
/////////VARS
var coptr:MovieClip = new copter();
var bulit:MovieClip = new bullit();
var mouseIsDn = false;
var speed = 0;
var PEW = false
var meter:MovieClip = new meters();
var bltArray:Array = new Array();
var airArray:Array = new Array();
var gndArray:Array = new Array();
var gameTIMERa:Timer = new Timer(5000);
var gameTIMERb:Timer = new Timer(10000);
stage.addEventListener(MouseEvent.MOUSE_DOWN, clicked);
stage.addEventListener(MouseEvent.MOUSE_UP, unclicked);
function clicked(e:Event):void{
mouseIsDn = true;
}
function unclicked(e:Event):void{
mouseIsDn = false;
}
/////////INTRO SCREEN
var titl:MovieClip = new title();
addChild(titl);
var strt:MovieClip = new start();
addChild(strt);
var govr:MovieClip = new gOVER();
var ctINTRO:Color = new Color();
ctINTRO.setTint(Math.random()*0xFFFFFF, 0.5);
BG.transform.colorTransform = ctINTRO;
/////////MUSIC
var ChanAB:SoundChannel = new SoundChannel();
var Amusic:Sound = new musicA();
var Bmusic:Sound = new musicB();
ChanAB = Amusic.play(0, 9999);
/////////SFx
var ChanSFx:SoundChannel = new SoundChannel();
var Ashoot:Sound = new shootA();
var SFx:Array = new Array();
var Asuck:Sound = new suckA();
SFx.push(Asuck);
var Bsuck:Sound = new suckB();
SFx.push(Bsuck);
var Csuck:Sound = new suckC();
SFx.push(Csuck);
var gOver:Sound = new over();
/////////START
strt.startBTN.addEventListener(MouseEvent.CLICK, str);
function str(e:Event):void{
removeChild(titl);
removeChild(strt);
ChanAB.stop();
BG.transform.colorTransform = new ColorTransform;
////////////
ChanAB = Bmusic.play(0, 9999);
addChild(coptr);
TweenLite.to(coptr, 3, {x:157, y:316});
addChild(meter);
meter.x = 861;
meter.y = 9;
TweenLite.to(meter, 1, {x:735});
meter.life.gotoAndPlay(2);
gameTIMERa.addEventListener(TimerEvent.TIMER, addAIR);
gameTIMERa.start();
gameTIMERb.addEventListener(TimerEvent.TIMER, addGND);
gameTIMERb.start();
}
/////////ADDING ENEMIES
function addAIR(e:TimerEvent):void{
var Aair:MovieClip = new airA();
Aair.x = 805;
Aair.scaleX = .25
Aair.y = Math.random() * stage.stageHeight - Aair.height;
Aair.scaleY = .25
airArray.push(Aair);
addChild(Aair);
}
function addGND(e:TimerEvent):void{
var Agnd:MovieClip = new gndA();
Agnd.x = 805;
Agnd.scaleX = .25
Agnd.y = 430 + Math.floor(Math.random() * 36);
Agnd.scaleY = .25
gndArray.push(Agnd);
addChild(Agnd);
}
addEventListener(Event.ENTER_FRAME, startLoop);
function startLoop(e:Event):void{
if(coptr.x == 157 && coptr.y == 316){
speed = 10;
meter.life.gotoAndStop(10);
removeEventListener(Event.ENTER_FRAME, startLoop);
addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, shoot);
}
}
/////////COPTER SHOOTING
function shoot(e:Event):void{
if(PEW == false){
ChanSFx = Ashoot.play();
bulit.x = coptr.x + 5;
bulit.y = coptr.y;
bltArray.push(bulit);
addChild(bulit);
PEW = true;
}
}
/////////LIFE METER
function lifeMeterA(e:Event = null):void{
if(meter.life.width > 59){
meter.life.scaleX -= .04;
}
else if(meter.life.width < 59 && meter.life.width > 29){
meter.life.gotoAndStop(11);
meter.life.scaleX -= .04;
}
else if(meter.life.width < 29 && meter.life.width > 15){
meter.life.gotoAndStop(12)
meter.life.scaleX -= .04;
}
else if(meter.life.width < 15 && meter.life.width > 1.5){
meter.life.gotoAndPlay(2);
meter.life.scaleX -= .04;
}
}
function lifeMeterB(e:Event = null):void{
if(meter.life.width > 59){
meter.life.scaleX -= .01;
}
else if(meter.life.width < 59 && meter.life.width > 29){
meter.life.gotoAndStop(11);
meter.life.scaleX -= .01;
}
else if(meter.life.width < 29 && meter.life.width > 15){
meter.life.gotoAndStop(12)
meter.life.scaleX -= .01;
}
else if(meter.life.width < 15 && meter.life.width > 1.5){
meter.life.gotoAndPlay(2);
meter.life.scaleX -= .01;
}
}
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////MAIN LOOP
function gameLoop(e:Event) {
////////////////////////COPTER MOVEMENT
trace(bltArray);
trace(airArray);
trace(gndArray);
coptr.y += speed;
if(mouseIsDn){
if(speed > -5){
speed -= 1;
}
}else{
if(speed < 10){
speed += .25;
}
}
////////////////////////BULLET MOVEMENT
if(bulit){
bulit.x += 10;
}
if(coptr.y > stage.stageHeight - coptr.height*.5){
coptr.y = stage.stageHeight - coptr.height*.5;
}
else if(coptr.y < 0 + coptr.height*.5){
coptr.y = 0 + coptr.height*.5;
}
////////////////////////COLLISIONS
for(var i = 0; i<numChildren; i++){
if(getChildAt(i) is airA){
var b = getChildAt(i) as airA;
if(b.hitTestObject(coptr)){
lifeMeterA();
}
if(b.hitTestObject(bulit)){
airArray.shift();
removeChild(b);
removeChild(bltArray[0]);
PEW = false;
var m:uint = uint(Math.random() * 3);
ChanSFx = SFx[m].play();
}
}
else if(getChildAt(i) is gndA){
var c = getChildAt(i) as gndA;
if(c.hitTestObject(coptr)){
lifeMeterB();
}
if(c.hitTestObject(bulit)){
gndArray.shift();
removeChild(c);
removeChild(bltArray[0]);
PEW = false;
var n:uint = uint(Math.random() * 3);
ChanSFx = SFx[n].play();
}
}
}
if(coptr.y > stage.stageHeight - coptr.height){
lifeMeterA();
}
////////////////////////CLEANUP MISSED ENEMIES
if(airArray[0] && airArray[0].x < 0){
airArray.shift();
}
if(gndArray[0] && gndArray[0].x < 0){
gndArray.shift();
}
////////////////////////BULLET RATE OF SHOOTING
if(bltArray[0] && bltArray[0].x > stage.stageWidth){
PEW = false;
bltArray.shift();
}
////////////////////////END GAME
if(meter.life.width < 1.5){
removeEventListener(Event.ENTER_FRAME, gameLoop);
gameTIMERa.stop();
gameTIMERb.stop();
meter.life.gotoAndStop(13);
TweenLite.to(meter, .5, {x:861});
gameO();
}
}
function gameO(e:Event = null):void{
addChild(govr);
govr.x = 0;
govr.y = 0;
ChanAB.stop();
ChanSFx = gOver.play();
ChanSFx.addEventListener(Event.SOUND_COMPLETE, restart);
}
function restart(e:Event):void{
removeChild(govr);
removeChild(coptr);
boot();
}
}
}
}
Thanks in advance!
It means you're using removeChild() somewhere on an object whose parent is not the container you are trying to remove it from.
For example, this code would cause that error:
var shape:Shape = new Shape();
stage.removeChild(shape);
It's likely that you are calling removeChild() more than once somewhere in your code.

Making an HTML Jigsaw Puzzle in Flash CC [duplicate]

I am am doing this for a interactive assignment for a media arts class and I have no idea how to code in action script 3. I took the orignal code from a tutorial and it didn't work so I came here and attempted to learn how to modify it.
AS3 Code
//*********************
// Initialize:
flash.events.MouseEvent
var numPieces = 16;
for (var i = 0; i < numPieces; i++)
{
var pieceName = "p" + (i + 1);
var piece = this[pieceName];
if( piece ){
piece.name = pieceName;
piece.addEventListener(MouseEvent.MOUSE_DOWN, function)(evt)
{
this.scaleX = 1;
this.scaleY = 1;
this.shadow = null;
this.parent.addChild(this);// Bump to top
this.offset = {x:this.x - evt.stageX, y:this.y - evt.stageY};
});
piece.addEventListener(MouseEvent.MOUSE_MOVE, function)
{
this.x = evt.stageX + this.offset.x;
this.y = evt.stageY + this.offset.y;
});
piece.addEventListener(MouseEvent.MOUSE_UP, function)(evt)
{
var target = this.parent["t"+this.name.substr(1)];
if( target && hitTestInRange( target, 30) ){
this.x = target.x;
this.y = target.y;
}
});
}
}
function hitTestInRange( target, range )
{
if( target.x > stage.mouseX - range &&
target.x < stage.mouseX + range &&
target.y > stage.mouseY - range &&
target.y < stage.mouseY + range )
{
return true;
}
return false;
}
Please help me fix this code so I can run my puzzle and move the pieces.
I can upload the flash file if needed

Can't interact with tiles in an interactive jigsaw puzzle (Flash CC AS3)

I am am doing this for a interactive assignment for a media arts class and I have no idea how to code in action script 3. I took the orignal code from a tutorial and it didn't work so I came here and attempted to learn how to modify it.
AS3 Code
//*********************
// Initialize:
flash.events.MouseEvent
var numPieces = 16;
for (var i = 0; i < numPieces; i++)
{
var pieceName = "p" + (i + 1);
var piece = this[pieceName];
if( piece ){
piece.name = pieceName;
piece.addEventListener(MouseEvent.MOUSE_DOWN, function)(evt)
{
this.scaleX = 1;
this.scaleY = 1;
this.shadow = null;
this.parent.addChild(this);// Bump to top
this.offset = {x:this.x - evt.stageX, y:this.y - evt.stageY};
});
piece.addEventListener(MouseEvent.MOUSE_MOVE, function)
{
this.x = evt.stageX + this.offset.x;
this.y = evt.stageY + this.offset.y;
});
piece.addEventListener(MouseEvent.MOUSE_UP, function)(evt)
{
var target = this.parent["t"+this.name.substr(1)];
if( target && hitTestInRange( target, 30) ){
this.x = target.x;
this.y = target.y;
}
});
}
}
function hitTestInRange( target, range )
{
if( target.x > stage.mouseX - range &&
target.x < stage.mouseX + range &&
target.y > stage.mouseY - range &&
target.y < stage.mouseY + range )
{
return true;
}
return false;
}
Please help me fix this code so I can run my puzzle and move the pieces.
I can upload the flash file if needed

Error 2025 when trying to removeChild, child not contained where it was added?

I am trying to code a tile-based level editor, in which the Main class adds Tile class instances as children of the 'tiles' movieclip when clicking/dragging the mouse.
I am able to add tiles to the container, and they show on stage, however I cannot remove any tiles when erasing them is enabled. It gives me the following error
Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Main/Draw()
at Main/::Go()
Also, when I check if the tile is inside the tiles container, it tells me that the parent is null.
So, a little help? I tried checking other questions with similar issues but none seemed to be close to mine.
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
//import flash.events.TimerEvent.updateAfterEvent;
public class Main extends MovieClip {
//containers
var lines:Sprite = new Sprite();
var tiles:Sprite = new Sprite();
// Grid data
var tileW:int = 20;
var tileH:int = 20;
var gridW:int = 20;//(inputWidth);
var gridH:int = 20;//(inputHeight);
var gridX:int = 50;
var grixY:int = 50;
var level:Array;
//Drawing variables
var go:Boolean = false;
var erase:Boolean = false;
var default_tile:int = 1;
var type:int;
var rect:Object = {x:100, y:50, width:(gridW * tileW)/100, height:(gridH * tileH)/100};
//menus
var sizeMenu:SizeMenu = new SizeMenu();
var current:Tile = new Tile();
public function Main():void {
//Flash alignment and resizing
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
stage.addChild(lines);
lines.x = rect.x;
lines.y = rect.y;
stage.addChild(tiles);
tiles.x = rect.x;
tiles.y = rect.y;
stage.addChild(sizeMenu);
stage.addChild(current);
current.x = 50;
current.gotoAndStop(default_tile);
stage.addEventListener(MouseEvent.MOUSE_DOWN, Go);
stage.addEventListener(MouseEvent.MOUSE_UP, Go);
stage.addEventListener(MouseEvent.MOUSE_MOVE, Go);
stage.addEventListener(KeyboardEvent.KEY_DOWN, ToggleErase);
stage.addEventListener(KeyboardEvent.KEY_UP, ToggleErase);
Setup();
}
//Draws grid lines
private function Setup():void {
trace("Drawing Grid...");
// create an empty array
level = new Array(gridH);
for (var i=0; i < gridW; i++) {
level[i] = new Array(gridW);
}
// attach lines to create a grid
for (var k=0; k <= gridH; k++) {
var line = new Line();
line.name = "line"+k;
line.scaleX = rect.width;
line.y = tileH * k;
lines.addChild(line);
for (var j=0; j <= gridW; j++) {
line = new Line();
line.name = "line"+j+"_"+k;
line.scaleX = rect.height;
line.x = tileW * j;
line.rotation = 90;
lines.addChild(line);
}
}
type = default_tile;
trace("Done drawing grid!");
}
//Decided if drawing is possible
private function Go(e:MouseEvent):void {
if (e.type == "mouseDown") {
go = true;
Draw(e);
}
if (e.type == "mouseUp") {
go = false;
}
if (e.type == "mouseMove") {
if (go) {
Draw(e);
}
//e.updateAfterEvent();
}
}
//Toggles erase
private function ToggleErase(e:KeyboardEvent):void{
if (e.shiftKey){
erase = true;
}
if (e.type == "keyUp"){
erase = false;
}
}
// attaches the tiles when drawn on the grid
public function Draw(e:MouseEvent) {
var x = mouseX;
var y = mouseY;
var cx = Math.floor((x - rect.x) / tileW);
var cy = Math.floor((y - rect.y) / tileH);
if (cx >= 0 && cx < gridW && cy >= 0 && cy < gridH) {
var target = e.currentTarget;
if (!erase) {
if (tiles.contains(target)){
trace("Contained!");
tiles.removeChild(target);
}
var tile = new Tile();
tiles.addChild(tile);
tile.name = ("t_" + cy + "_" + cx);
tile.x = (tileW * cx);
tile.y = (tileH * cy);
tile.gotoAndStop(type);
level[cy][cx] = type;
} else {
if (tiles.contains(target)){
trace("Contained!");
tiles.removeChild(target);
}
level[cy][cx] = default_tile - 1;
}
}
}
//Cleans the grid and redraws it
private function ResetGrid():void {
level = null;
//Delete tiles
while (tiles.numChildren) {
tiles.removeChildAt(0);
}
//Delete lines
while (lines.numChildren) {
lines.removeChildAt(0);
}
gridW=20;
gridH=20;
rect.width = (gridW * tileW)/100;
rect.height = (gridH * tileH)/100;
}
// updates the current-clip
private function update() {
current.gotoAndStop(type);
}
}
}
The following code is causing the problem, basically you are calling removeChild twice for the same object.
if (tiles.contains(target)){
trace("Contained!");
tiles.removeChild(target);
}
tiles.removeChild(target);
In your code, I notice that you have the ability for the tile to be removed, and then try to remove it again at the end of this block :
if (!erase) {
if (tiles.contains(target)){
trace("Contained!");
tiles.removeChild(target);
}
var tile = new Tile();
tiles.addChild(tile);
tile.name = ("t_" + cy + "_" + cx);
tile.x = (tileW * cx);
tile.y = (tileH * cy);
tile.gotoAndStop(type);
level[cy][cx] = type;
} else {
if (tiles.contains(target)){
trace("Contained!");
tiles.removeChild(target);
}
// this is going to throw an error
tiles.removeChild(target);
Make it easy on yourself and create a class for Tile with a remove method. Something like this:
class Tile extends Sprite
{
public function remove():void
{
if(parent) parent.removeChild(this);
}
}
This way, you can simply do:
tile.remove();
I have resolved my issue! All tile instances are given a name based on their position on the grid when added. Instead of making target the object the mouse was pointing at, I used getChildByName(); to search if there was already an object with a specific name, and to erase it if it did.
if (cx >= 0 && cx < gridW && cy >= 0 && cy < gridH) {
var target = tiles.getChildByName("t_" + cy + "_" + cx);
if (!erase) {
if (target){
tiles.removeChild(target);
}
var tile = new Tile();
tiles.addChild(tile);
tile.name = ("t_" + cy + "_" + cx);
tile.x = (tileW * cx);
tile.y = (tileH * cy);
tile.gotoAndStop(type);
level[cy][cx] = type;
} else {
if (target){
tiles.removeChild(target);
}
level[cy][cx] = default_tile - 1;
}
}

How to get MovieClip.prototype in ActionScript 3?

In a ActionScript 2 code sample it uses some MovieClip.prototype's functions, as MovieClip.prototype.setModel;
I'm trying to write this example using an ActionScript 3 class, but the MoviewClip.prototype does not exists, where can I get it from? Does it exist in ActionScript 3?
--update
Ok, as you are asking, here goes, this code works just fine as an ActionScript included on frame1, but I want to make a ActionScript 3 class with this same code:
//package
//{
// public class asteroids extends MovieClip
// {
var MW = 8; // Scaling factor for models (which were originally drawn on graph paper)
var SW = Stage.width; // Stage coords
var SH = Stage.height;
var kDegToRad = Math.PI/180; // Useful constant for drawing circles & such
var kDamp = 0.99; // Damping in ship acceleration
// The models
//
// Rocket fuselage
var fuseModel = [{mx:0, my:-5.5, x:0, y:-4, pen:.5, clr:0x000033, alpha:50},
{cx:-1,cy:-3,x:-1,y:-1, pen:2, clr:0x000033, alpha:50},
{x:-.75,y:4},
{cx:0, cy:4.5, x:.75,y:4}, // from -.75,4
{x:1,y:-1},
{cx:1,cy:-3,x:0,y:-4}];
// Rocket fins
var finModel = [ {mx:-1,my:-1,cx:-3,cy:4,x:-2,y:6,bf:0x0000FF,bfa:80},
{cx:-1,cy:4.5,x:-.75,y:4},
{x:-1,y:-1},
{mx:.75,my:4,cx:1,cy:4.5,x:2,y:6},
{cx:3,cy:4,x:1,y:-1},
{x:.75,y:4,ef:1}
];
// Routine to scale model to arbitrary size
function scaleModel(m,s)
{
for (var i = 0; i < m.length; ++i)
{
var pt = m[i];
if (pt.mx != undefined)
{
pt.mx *= s;
pt.my *= s;
}
if (pt.cx != undefined)
{
pt.cx *= s;
pt.cy *= s;
}
pt.x *= s;
pt.y *= s;
}
}
// Draw a model
//
function drawModel(m)
{
for (var i = 0; i < m.length; ++i)
{
var pt = m[i];
if (pt.bf != undefined)
this.beginFill(pt.bf, pt.bfa);
if (pt.pen != undefined)
this.lineStyle(pt.pen,pt.clr,pt.alpha);
if (pt.mx != undefined)
this.moveTo(pt.mx,pt.my);
if (pt.cx != undefined)
this.curveTo(pt.cx,pt.cy,pt.x,pt.y);
else if (pt.x != undefined)
this.lineTo(pt.x,pt.y);
if (pt.ef != undefined)
this.endFill();
}
}
// Ship Movement and most game-play stuff happens here
function ShipMove()
{
// Steering & Thrust
if (Key.isDown(Key.LEFT))
this._rotation -= 5;
else if (Key.isDown(Key.RIGHT))
this._rotation += 5;
if (Key.isDown(Key.CONTROL) || Key.isDown(Key.UP))
{
this.vx += Math.sin(this._rotation*kDegToRad);
this.vy -= Math.cos(this._rotation*kDegToRad);
}
else if (Key.isDown(Key.DOWN))
{
this.vx *= .9;
this.vy *= .9;
}
// Basic movement with acceleration and damping
this._x += this.vx;
this._y += this.vy;
// Wrap around edges of stage
if (this._x < -this._width)
this._x += SW+this._width*2;
else if (this._x > SW+this._width)
this._x -= SW+this._width*2;
if (this._y < -this._height)
this._y += SH+this._height*2;
else if (this._y > SH+this._height)
this._y -= SH+this._height*2;
}
// Initialize new ship
function NewShip()
{
ship_mc._x = SW/2;
ship_mc._y = SH/2;
ship_mc.onEnterFrame = shipMove;
}
// Assign a sprite model to a movieclip & draw it
//
MovieClip.prototype.setModel = function(m)
{
this.model = m;
this.drawModel(m);
}
MovieClip.prototype.drawModel = drawModel;
scaleModel(fuseModel, MW*.8); // Using a slightly smaller rocket than original drawing
scaleModel(finModel, MW*.8);
// One Time Initialziation
//
_root.createEmptyMovieClip("ship_mc", 2);
ship_mc.createEmptyMovieClip("fuselage", 1);
ship_mc.fuselage.setModel(fuseModel);
ship_mc.createEmptyMovieClip("fins", 2);
ship_mc.fins.setModel(finModel);
NewShip();
// }
//}
You should probably extend MovieClip and add the new functionality to the subclass, it's the best/proper way to go. Prototypes in AS2 are old, and were used when the language didn't support true OOP.
Using prototypes in AS3 is discouraged.
But there is a way to use it if you are compiling AS3 with the flex compiler.
First, you need to give a few compiler options.
PrototypeExtension-config.xml
<flex-config>
<compiler>
<as3>false</as3>
<es>true</es>
<strict>false</strict>
</compiler>
</flex-config>
PrototypeExtension.as
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.getTimer;
public class PrototypeExtension extends Sprite {
public function PrototypeExtension() {
stage.align = "tl";
stage.scaleMode = "noScale";
MovieClip.prototype.move = function (x:int, y:int):void {
this.x = x; this.y = y;
};
MovieClip.prototype.tween = function (to:Point, duration:int):void {
var start:int = getTimer();
var from:Point = new Point(this.x, this.y);
var thisInstance:MovieClip = this;
this.addEventListener(Event.ENTER_FRAME, function tweener(e:Event):void {
var time:int = getTimer() - start;
if (time > duration) {
thisInstance.move(to.x, to.y);
thisInstance.removeEventListener(Event.ENTER_FRAME, tweener);
}
var t:Number = time / duration;
t = t * (2 - t);
var u:Number = 1 - t;
thisInstance.move(u * from.x + t * to.x, u * from.y + t *to.y);
});
}
var mc:MovieClip = new MovieClip;
mc.graphics.beginFill(0);
mc.graphics.drawRect(0, 0, 100, 100);
mc.graphics.endFill();
mc.tween(new Point(400, 300), 1000);
addChild(mc);
}
}
}
by
mxmlc PrototypeExtension.as
You can see how prototype extension is enabled in AS3.