Error #1010 in level setup class - actionscript-3

I'm having problem with a piece of code of mine, and it seems to be in this following class:
package
{
import flash.display.MovieClip;
//levels are 11x9
public class Level extends MovieClip
{
var level1:Array=new Array(
1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,1,0,0,0,0,0,0,1,
1,0,0,1,1,1,0,0,0,1,1,
1,0,0,0,0,0,0,1,1,0,1,
1,1,1,1,1,1,1,1,1,1,1);
var grid:Array = new Array(11);
public function Level()
{
for (var i = 0; i < 9; i++)
{
grid.push(new Array(9));
}
for (var xr=0; xr<11; xr++)
{
for (var yr=0; yr<9; yr++)
{
var type = level1[yr * 11 + xr];
var obj:Wall = new Wall(xr*50,yr*50,type);
grid[xr][yr] = obj;
if (type!=0)
{
addChild(obj);
}
}
}
}
}
}
Now I've done some work, and the error is Error Code #1010: Term undefined and has no properties.
Even more specific, I did some debugging and determined the exact line to be
grid[xr][yr]=obj;
Any help is appreciated.
enter code here
enter code here

change
grid.push(new Array(9));
to
grid[i]=new Array(9);
[EDIT]
Actually try this
var level1:Array=new Array(
1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,1,
1,0,0,1,0,0,0,0,0,0,1,
1,0,0,1,1,1,0,0,0,1,1,
1,0,0,0,0,0,0,1,1,0,1,
1,1,1,1,1,1,1,1,1,1,1);
var grid:Array = new Array(11);
public function Level()
{
for(var row=0;col<11;row++){
grid[col] = new Array(9);
for(var col=0;col<9;col++){
var type = level1[row* 11 + col];
var obj:Wall = new Wall(col*50,row*50,type);
grid[col][row] = obj;
if (type!=0){
addChild(obj);
}
}
}
}

Related

Why can't I load this previously serialized object?

This class is serializable:
package com.things {
public class MySerializableObject {
public var intVals:Array;
public var numVals:Array;
public var stringVals:Array
public function MySerializableObject() {
}
public function init(ints:Vector.<int>, nums:Vector.<Number>, strings:Vector.<String>) {
intVals = new Array();
for each (var i:int in ints) {
intVals.push(i);
}
numVals = new Array();
for each (var n:Number in nums) {
numVals.push(n);
}
stringVals = new Array();
for each (var s:String in strings) {
stringVals.push(s);
}
}
}
}
We can do this:
registerClassAlias("com.things.MySerializableObject", MySerializableObject);
And then do this:
var sharedObject:SharedObject = SharedObject.getLocal(SAVE_NAME); //SAVE_NAME is a string constant
var mso:MySerializableObject = getInitializedMSO();
sharedObject.data.mso = mso;
sharedObject.flush();
var newMSO:MySerializableObject = sharedObject.data.mso as MySerializableObject;
trace(newMSO); //outputs "[Object MySerializableObject]"
However, if we restart the application and do this:
var sharedObject:SharedObject = SharedObject.getLocal(SAVE_NAME);
registerClassAlias("com.things.MySerializableObject", MySerializableObject);
var previouslySavedMSO:MySerializableObject = sharedObject.data.mso as MySerializableObject;
trace(previouslySavedMSO); //outputs "null"
Flash apparently fails to read the saved object as a MySerializableObject after we close and re-open the application. We can see, by inspecting the SO in a debugger, that the data is still there, but Flash can't typecast it back into its original data type anymore. Is this the expected behavior? If not, what is the problem and how can we fix it?
You have to register your class aliases before you read from your LSO (or byte stream, etc..) otherwise you will end up with an anonymous object. Since you are than casting via an as the result will be null.
Returns null on second run:
var sharedObject:SharedObject = SharedObject.getLocal(SAVE_NAME);
registerClassAlias("com.things.MySerializableObject", MySerializableObject);
if (sharedObject.size > 0) {
var newMSO:MySerializableObject = sharedObject.data.mso as MySerializableObject;
trace(newMSO);
} else {
sharedObject.data.mso = getInitializedMSO();
sharedObject.flush();
trace(sharedObject.data.mso);
}
Returns MySerializableObject on the second run:
registerClassAlias("com.things.MySerializableObject", MySerializableObject);
var sharedObject:SharedObject = SharedObject.getLocal(SAVE_NAME);
if (sharedObject.size > 0) {
var newMSO:MySerializableObject = sharedObject.data.mso as MySerializableObject;
trace(newMSO);
} else {
sharedObject.data.mso = getInitializedMSO();
sharedObject.flush();
trace(sharedObject.data.mso);
}
Cut/Paste Example:
package {
import flash.display.Sprite;
import flash.net.SharedObject;
import flash.net.registerClassAlias;
public class Main extends Sprite {
private const SAVE_NAME:String = "foobar";
public function Main() {
registerClassAlias("com.things.MySerializableObject", MySerializableObject);
var sharedObject:SharedObject = SharedObject.getLocal(SAVE_NAME);
if (sharedObject.size > 0) {
var newMSO:MySerializableObject = sharedObject.data.mso as MySerializableObject;
trace(newMSO);
} else {
sharedObject.data.mso = getInitializedMSO();
sharedObject.flush();
trace(sharedObject.data.mso);
}
}
public function getInitializedMSO():MySerializableObject {
return new MySerializableObject();
}
}
}
class MySerializableObject {
public var intVals:Array;
public var numVals:Array;
public var stringVals:Array
public function MySerializableObject() {
}
public function init(ints:Vector.<int>, nums:Vector.<Number>, strings:Vector.<String>) {
intVals = new Array();
for each (var i:int in ints) {
intVals.push(i);
}
numVals = new Array();
for each (var n:Number in nums) {
numVals.push(n);
}
stringVals = new Array();
for each (var s:String in strings) {
stringVals.push(s);
}
}
}

Making a reset placed objects button in Flash (as3)

I have a drag and drop project where everytime I click on a movieclip a clone is made that can be dragged. I would like to know how to make a button that can reset/remove the clones when the button is pressed.
This is what I got so far:
import flash.display.MovieClip;
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
function onPlusPressed(event:MouseEvent):void
{
latestClone = new Plus();
latestClone.x = event.stageX;
latestClone.y = event.stageY;
addChild(latestClone);
latestClone.startDrag();
latestClone.addEventListener(MouseEvent.MOUSE_DOWN, latestClone.startDrag);
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
if(latestClone != null){
latestClone.stopDrag();
}
}
Add all of your clones in an array, and loop over the array and do a removeChild on each item in the array. So:
var items:Array = new Array();
....
addChild(latestClone);
items.push(latestClone);
....
var resetButton:SimpleButton = new SimpleButton();
//set your button properties here
resetButton.addEventListener(MouseEvent.CLICK, onResetClicked);
addChild(resetButton);
function onResetClicked(e:MouseEvent):void
{
reset();
}
function reset():void
{
for (var i:uint = 0; i < items.length; i ++)
{
removeChild(items[i]);
items[i] = null;
}
items = new Array();
}
Hope this helps.
just declare container variable like so:
var clone_container:Sprite = new Sprite();
put all clones inside,than u can clear it up very easely:
while(clone_container.numChildren > 0){
clone_container.removeChildAt(0);
}
that's all..
I found the solution to my question by testing several possibilities with the code that was posted + searching other questions.
This is the code that worked:
import flash.display.MovieClip;
import flash.events.MouseEvent;
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
function onPlusPressed(event:MouseEvent):void
{
latestClone = new Plus();
latestClone.x = event.stageX;
latestClone.y = event.stageY;
addChild(latestClone);
latestClone.startDrag();
latestClone.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed);
}
function onClonedPlusPressed(event:MouseEvent):void{
latestClone = MovieClip(event.currentTarget);
latestClone.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
if(latestClone != null){
latestClone.stopDrag();
items.push(latestClone);
}
}
resetButton.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
}
var items:Array = new Array();
resetButton.addEventListener(MouseEvent.CLICK, onResetClicked);
addChild(resetButton);
function onResetClicked(e:MouseEvent):void
{
reset();
}
function reset():void
{
for (var i:uint = 0; i < items.length; i ++)
{ items[i].removeEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed);
if(items[i].parent) items[i].parent.removeChild(items[i]);
items[i] = null;
}
items = new Array()
}
you most add an array and push object into this,
your solution:
import flash.display.MovieClip;
import flash.events.Event;
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
var plusArray:Array = new Array();
resetbtn.addEventListener(MouseEvent.CLICK,resetFunc);
function resetFunc(e:Event)
{
for (var i=0; i<plusArray.length; i++)
{
removeChild(plusArray[i]);
}
plusArray = new Array()
}
function onPlusPressed(event:MouseEvent):void
{
latestClone = new Plus();
latestClone.x = event.stageX;
latestClone.y = event.stageY;
plusArray.push(latestClone);
addChild(plusArray[plusArray.length-1]);
plusArray[plusArray.length - 1].startDrag();
plusArray[plusArray.length - 1].addEventListener(MouseEvent.MOUSE_DOWN, plusArray[plusArray.length-1].startDrag);
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
if (latestClone != null)
{
latestClone.stopDrag();
}
}
Good luck

AS3: Class can't find another class to interact with

So I have this code inside of one of my enemy class for my flash game.
package
{
import flash.events.*;
import flash.display.*;
import flash.geom.*;
public class JumpBall extends MovieClip
{
var MTL:MovieClip = MovieClip(root);
var charMTL:MovieClip;
var bullet:Bullet = new Bullet;
public function JumpBall()
{
this.addEventListener(Event.ENTER_FRAME, EnemyBallUpdate);
}
function EnemyBallUpdate(event:Event):void
{
charMTL = MTL.char1;
var enemy:Rectangle = this.getBounds(this);
var enemy_matrix:Matrix = this.transform.matrix;
var enemyBitmap:BitmapData = new BitmapData(enemy.width, enemy.height, true, 0);
enemyBitmap.draw(this,enemy_matrix);
enemy_matrix.tx = this.x - enemy.x;
enemy_matrix.ty = this.y - enemy.y;
var char:Rectangle = charMTL.getBounds(this);
var char_matrix:Matrix = charMTL.transform.matrix;
var charBitmap:BitmapData = new BitmapData(char.width, char.height, true, 0);
charBitmap.draw(charMTL,char_matrix);
char_matrix.tx = charMTL.x - char.x;
char_matrix.ty = charMTL.y - char.y;
var enemyPoint:Point = new Point(enemy.x, enemy.y);
var charPoint:Point = new Point(char.x, char.y);
if(enemyBitmap.hitTest(enemyPoint, 0, charBitmap, charPoint, 0) && MTL.currentFrame == 2)
{
if(MTL.HP == 0)
{
MTL.RemoveListeners();
MTL.vcam_2.x = 400.2;
MTL.vcam_2.y = 224.9;
MTL.gotoAndStop(3);
MTL.HP = 3;
MTL.CC = 0;
MTL.removeChild(charMTL);
}else{
MTL.removeKeyboardEvts();
MTL.KeysOFF();
MTL.fade_mc.gotoAndPlay(2);
charMTL.gotoAndStop(1);
MTL.ySpeed = 0;
MTL.removeChild(charMTL);
}
}
enemyBitmap.dispose();
charBitmap.dispose();
}
public function removeJumpBallEL(event:Event):void
{
this.removeEventListener(Event.ENTER_FRAME, EnemyBallUpdate);
}
}
}
Upon entering frame 2 I get the silly error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at JumpBall/EnemyBallUpdate()
I can't find out why the class can't talk to the char1 MovieClip (character of my game).
I have the same type of class for a reset box with the exception that this class uses .hitTestPoint method. No errors there.
I'm trying to add my enemies to an array for interaction with a bullet class.
Here's the main timeline code:
function addJumpBall():void
{
var jumpball:JumpBall = new JumpBall;
jumpball.x = 673;
jumpball.y = 318;
addChild(jumpball);
jumpball.addEventListener(Event.REMOVED, jumpballRemoved);
JumpBallArray.push(jumpball);
}
function jumpballRemoved(event:Event):void
{
event.currentTarget.removeEventListener(Event.REMOVED, jumpballRemoved);
JumpBallArray.splice(JumpBallArray.indexOf(event.currentTarget), 1);
}
addJumpBall();
Thanks to anybody willing to help me!

Line 39 1067: Implicit coercion of a value of type String to an unrelated type Enemy

First I had no compiler errors but my for EACH was not working
for each( var enemy:Enemy in army )
{
enemy.moveDownAbit();
}
I replaced the for EACH with just for and I have been getting this error.
and I have no idea how to fix it as I am new to AS3.
This is my Enemy.as
public class Enemy extends MovieClip
{
public function Enemy(startX:Number, startY:Number)
{
x=startX;
y=-startY;
}
public function moveDownAbit(): void
{
y=y+3;
}
}
If you just replaced for-each with for it is not enough.
The following sample
for var enemy:Enemy in army )
{
enemy.moveDownAbit();
}
Will indeed try to read all the "properties" of Army and not loop through the list of them.
This is helpful when you want for example to print the properties/values of an object
Ex:
class Person
{
public var name:String = "Object name";
public var age:int = 20;
}
function doForLoop()
{
var myPers:Person = new Person();
// here your loop
for (var prop:String in myPers)
{
trace(prop+" = "+myPers[prop]);
}
// ---- previus loop will trace:
// name = Object name
// age = 20
// whet you actually need is
for(var i:int = 0 ; i < army.length ; i++)
{
var enemy:Enemy = army[i] as Enemy;
// do your work now
enemy.moveDownAbit();
}
}
Hope explanation helps.

Stage properties in custom class, not Document Class

I need to use stage.width/height in my CustomClass so I found some topics about it.
if (stage)
{
init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
But in my case it won't work because it isn't document class, I think. Any other solution?
UPDATE:
CLASS CODE
package
{
import fl.transitions.Tween;
import fl.motion.easing.*;
import flash.filters.*;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.ui.Mouse;
import flash.display.*;
public class Wyjazd extends MovieClip
{
public function Wyjazd(ar:Array=null,firma:Object=null,kontakt:Object=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null)
{
if (stage)
{
//The stage reference is present, so we're already added to the stage
init(ar,firma,kontakt,oferta,naglowek,tekst,dane);
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
public function init(ar:Array,firma:Object=null,kontakt=null,oferta:Object=null,naglowek:Object=null,tekst:Object=null,dane:Object=null):void
{
//Zmienne "globalne" dla funkcji
var time:Number;
var wciecie:Number;
var wciecie2:Number;
var offset:Number = 15.65;
var offset2:Number = 20;
var posX:Array = new Array(12);
var posY:Array = new Array(12);
var spr:Array = new Array(12);
var targetLabel:String;
var wybranyOb:Object = ar[0];
var names:Array = new Array('Szkolenie wstępne BHP','Szkolenie okresowe BHP','Szkolenie P.Poż','Kompleksowa obsługa P.Poż','Pomiar środowiska pracy','Szkolenie z udzielania pierwszej pomocy','Ocena ryzyka zawodowego','Przeprowadzanie postępowań po wypadkowych','Przeprowadzanie audytów wewnętrznych ISO','Hałas w środowisku komunalnym','Medycyna pracy','Szkolenia dla kierowców');
//Pobieranie pozycji
for (var i:Number = 0; i<ar.length; i++)
{
posX[i] = ar[i].x;
posY[i] = ar[i].y;
}
//Filtry
function increaseBlur(e:MouseEvent,docPos:Number):void
{
var myBlur:BlurFilter =new BlurFilter();
myBlur.quality = 3;
myBlur.blurX = 10;
myBlur.blurY = 0;
}
//Funkcje
function startPos():void
{
time = 0.2;
for (var i:Number = 0; i<ar.length; i++)
{
//if (wybranyOb.name == ar[i].name)
//{
//var wybranyPos:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],0.01,true);
//wybranyPos = new Tween(ar[i],"y",Linear.easeOut,-30,posY[i],time,true);
//}
//else
//{
var position:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
position = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
//}
//time = 0.2;
}
position = new Tween(naglowek,"x",Linear.easeOut,naglowek.x,2000,time,true);
position = new Tween(tekst,"x",Linear.easeOut,tekst.x,2000,time,true);
position = new Tween(dane,"x",Linear.easeOut,dane.x,2000,0.25,true);
}
//Nasłuchy
oferta.addEventListener(MouseEvent.CLICK, wyskokOferta);
oferta.addEventListener(MouseEvent.MOUSE_OVER,glowOferta);
oferta.addEventListener(MouseEvent.MOUSE_OUT,unglowOferta);
kontakt.addEventListener(MouseEvent.CLICK,wyskokKontakt);
kontakt.addEventListener(MouseEvent.MOUSE_OVER,glowKontakt);
kontakt.addEventListener(MouseEvent.MOUSE_OUT,unglowKontakt);
firma.addEventListener(MouseEvent.CLICK,wyskokFirma);
firma.addEventListener(MouseEvent.MOUSE_OVER,glowFirma);
firma.addEventListener(MouseEvent.MOUSE_OUT,unglowFirma);
function glowFirma(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
firma.filters = [myGlow];
}
function unglowFirma(e:MouseEvent):void
{
firma.filters = [];
}
function glowKontakt(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
kontakt.filters = [myGlow];
}
function unglowKontakt(e:MouseEvent):void
{
kontakt.filters = [];
}
function glowOferta(e:MouseEvent):void
{
var myGlow:GlowFilter=new GlowFilter();
myGlow.color = 0xe6da13;
myGlow.inner = true;
oferta.filters = [myGlow];
}
function unglowOferta(e:MouseEvent):void
{
oferta.filters = [];
}
function wyskokKontakt(e:MouseEvent):void
{
startPos();
var tweenKontakt = new Tween(dane,"x",Linear.easeOut,2000,350,0.25,true);
}
function wyskokFirma(e:MouseEvent):void
{
startPos();
trace("Firma");
}
function wyskokOferta(e:MouseEvent):void
{
time = 0.2;
wciecie = 15.65;
wciecie2 = 20.05;
for (var i:Number = 0; i < ar.length; i++)
{
var tween:Tween = new Tween(ar[i],"x",Sine.easeOut,ar[i].x,oferta.x + wciecie,time,true);
tween = new Tween(ar[i],"y",Sine.easeOut,ar[i].y,oferta.y + wciecie2,time,true);
ar[i].addEventListener(MouseEvent.CLICK,onClick);
spr[i] = i;
time += 0.02;
wciecie += offset;
wciecie2 += offset2;
}
}
function onClick(e:MouseEvent)
{
startPos();
time = 0.2;
var k:Number = 0;
targetLabel = e.currentTarget.name;
for (var i:Number = 0; i < ar.length; i++)
{
if (targetLabel==ar[i].name)
{
//wybranyOb = ar[i];
var tween:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
tween = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
tween = new Tween(naglowek,"x",Linear.easeOut,2000,60,0.2,true);
tween = new Tween(tekst,"x",Linear.easeOut,2000,500,0.25,true);
naglowek.text = names[i];
}
else
{
var tween1:Tween = new Tween(ar[i],"x",Linear.easeOut,ar[i].x,posX[i],time,true);
tween1 = new Tween(ar[i],"y",Linear.easeOut,ar[i].y,posY[i],time,true);
}
//time += 0.02;
}
}
}
}
}
Hope it will helps.
I expect you are getting an error from the init function which expects lots of parameters but might just get one Event. It helps when you post here if you post the compile or runtime errors you are getting along with source code.
I think this should work for you, I've made a rough version which you can learn from and apply to your own class
public class CustomClass extends MovieClip
{
protected var _company:String;
protected var _data:Object;
public function CustomClass( company:String='', data:Object=null )
{
_company = company;
_data = data;
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event=null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//do something with _data
//do something with _company
}
}
Hopefully you can see the concept here, place your constructor variables in class variables when you create the class, then if on the stage call init() which uses those class variables or add an event listener which will call init(passing in an event this time) and then use the same class variables to do what you want.
Note how I remove the event listener when it isn't needed any more as well.