Building Sokoban Game using Actionscript 3, and getting incorrect number of arguments - actionscript-3

I've been doing this Sokoban game for the university, however, I'm stuck with something. Here's the code so far on AS3:
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
private var levels:Array=new Array();
private var currentLevel:uint = 0;
private var tile:tiles_mc;
private var level_container:Sprite;
public function Main()
{
setupLevels();
drawLevel(currentLevel);
}
private function setupLevels():void
{
levels[0]=[[1,1,1,1,0,0,0,0],[1,0,0,1,1,1,1,1],[1,0,2,0,0,3,0,1],
[1,0,3,0,0,2,4,1],[1,1,1,0,0,1,1,1],[0,0,1,1,1,1,0,0]];
levels[1]=[[0,0,1,1,1,1,0,0],[0,0,1,0,0,1,0,0],[1,1,1,0,0,1,1,1],
[1,0,3,5,2,4,0,1],[1,0,0,0,0,0,0,1],[1,1,1,1,1,1,1,1]];
levels[2]=[[1,1,1,1,1,1,1,1],[1,0,0,1,0,0,0,1],[1,0,0,0,0,0,0,1],
[1,3,3,3,1,1,1,1],[1,6,2,0,2,1,0,0],[1,1,1,1,1,1,0,0]];
trace("My levels: "+levels);
}
private function drawLevel(level:uint):void
{
level_container = new Sprite();
addChild(level_container);
for (var i:uint=0; i<levels[currentLevel].length; i++)
{
for (var j:uint=0; j<levels[currentLevel][i].length; j++)
{
switch (levels[currentLevel][i][j])
{
case 0 :// floor
tile = new tiles_mc(1,i,j);
level_container.addChild(tile);
break;
case 1 :// wall
tile = new tiles_mc(1,i,j);
level_container.addChild(tile);
break;
case 2 :// crate goal
tile = new tiles_mc(1,i,j);
level_container.addChild(tile);
break;
}
}
}
}
}}
However, when I try to test, I get this error:
What's going on? Clearly AS recognises the arguments, as seen here:
What am I doing wrong?

Related

AS3: Why is a line created with .graphics appearing in two different places and when removed with parent.visible = false, only one goes?

Nobody seems to have this question already so I asked it because I've spent a few hours trying to debug this and can't find a solution;
Essentially, I have a function called draw, which is declared in my document class:
public function draw(Target: MovieClip,mX: int,mY: int,lX: int,lY: int):void {
Target.graphics.clear();
Target.graphics.lineStyle(1,0x000000,1);
Target.graphics.moveTo(mX,mY);
Target.graphics.lineTo(lX,lY);
}
I call it later to draw two lines, on two different MovieClips:
draw(Line,Line.mX,Line.mY,Mirror.x + (Mirror.width / 2),Line.lY);
draw(nextLine,(Mirror.x + (Mirror.width / 2)),200,(Mirror.x + (Mirror.width / 2)),0);
where
var Line: MovieClip = new MovieClip();
var Mirror: MovieClip = new mirror();
and Mirror is draggable, so Mirror.x changes whenever it is dragged.
Line is a line made using .graphics and Line.mX is equal to the Line.graphics.moveTo X value last time it was modified. Line.mY is the same, but for the Y coordinate. I set these values by doing this:
Line.mX = 0;
Line.mY = 200;
Line.lX = 550;
Line.lY = 200;
But with whatever values I want to draw the line, with lX and lY being equal to the X and Y coordinates of Line.graphics.lineTo. Then I draw Line using my draw function like this:
draw(Line,Line.mX,Line.mY,Line.lX,Line.lY);
Then it gets more complex because, actually, Line is just one line in an array of lines, created like this:
public var lines = [line0,line1,line2,line3,line4,line5,line6,line7,line8];
and each of those lines is created like this (with 0 being replaced by the line's number, respectively):
public var line0: MovieClip = new MovieClip();
then I give each line a number and a name, add them to the stage and hide them like this:
for each(var setupLine:MovieClip in lines) {
setupLine.num = (lines.indexOf(setupLine));
setupLine.name = ('line' + setupLine.num);
addChild(setupLine);
setupLine.visible = false;
}
Then, after making line0 visible, because I need to see it at the start, I loop through each line in a function that runs on ENTER_FRAME, and set the value of nextLine to a different value each time I run the loop like this:
for each(var Line:MovieClip in lines) {
nextLine = this['line' + (Line.num + 1)];
}
Within that loop, I then loop through a few other arrays, then check for a collision with the selected Line and another selected MovieClip from another array, which I wont go into or this question will be longer than the code for node.js.
So essentially, if the collision with the two MovieClips is present, I draw the line that I mentioned at the top of my question. But for some reason, although Line draws correctly, nextLine draws correctly, but a duplicate of it is drawn across the Y axis at 0, and stops where nextLine is on the Y axis (nextLine is vertical, so it has the same Y value at the start as at the end).
Even stranger, when I try to hide nextLine if the collision with the two MovieClips is no longer present, using this code:
nextLine.visible = false;
it only hides the version of nextLine that runs along the top of the stage, which I didn't even intend to create in the start.
EDIT
here is a link to the current source code
Here is a link to the entire project files with the original source code
copy/paste the new source code from the pastebin link to get the new version
Thanks in advance,
-Raph
I figured out how to do this, code is
package {
import flash.events.*;
import flash.utils.*;
import flash.display.*;
[SWF(backgroundColor="0xbdc3c7")]
public class LightStage extends MovieClip {
//import classes
public var globeClass:Globe = new Globe();
public var mirrorClass:Mirror = new Mirror();
public var lineClass:Line = new Line();
//create all stage objects
public var curLine:Line
public var nextLine:Line;
public var curMirror:Mirror;
//create containers
public var mirrors:Vector.<Mirror> = new Vector.<Mirror>(); //a vector is an array, but every member has to be (or subclass) the specified class
public var globes:Vector.<Globe> = new Vector.<Globe>();
public var lines:Vector.<Line> = new Vector.<Line>();
trace('lightstage: working');
//create level object
public var curLevel:int = -1;
//create dependent variables
public var kill: Boolean = true;
//init function
public function LightStage() {
//setup MovieClips
var i:int = 0;
for (i = 0; i < 4; i++) {
mirrors.push(new Mirror());
}
for (i = 0; i < 4;i++ ) {
globes.push(new Globe());
}
var tmpLine:Line;
for (i = 0; i < 10; i++) {
tmpLine = new Line();
lines.push(tmpLine);
addChild(tmpLine);
tmpLine.visible = false;
}
//create ENTER_FRAME listener
stage.addEventListener(Event.ENTER_FRAME,enterFrame);
//start the game
levelUp();
}
//levelUp function
public function levelUp() {
curLevel++;
curLine = lines[curLevel]; //set line to the current level
curLine.curX = 0;
curLine.curY = 200;
curLine.draw(550, 200);
curLine.visible = true;
//show and position mirrors and globes
curMirror = mirrors[curLevel];
addChild(curMirror);
curMirror.x = 250;
curMirror.y = 350;
var curGlobe:Globe = globes[curLevel];
addChild(curGlobe);
curGlobe.x = 100;
curGlobe.y = 50;
//set mirror types
curMirror.gotoAndStop(2);
trace("you are now on level " + (curLevel + 1) + "!");
}
//ENTER_FRAME function
public function enterFrame(event:Event) {
//line1.visible = true;
for (var i:int = 0; i < lines.length;i++){
if (i < lines.length - 1) nextLine = lines[i + 1]; //check for out of bounds before assignment next line
if (lines[i].visible == true) {
kill = true;
for each(var mirror:Mirror in mirrors) {
if (lines[i].visible && mirror.stage && mirror.hitTestObject(lines[i])) { //for efficiency, do the hit test last in the if statement
for each(var globe:Globe in globes) {
//Looped through Mirrors and Lines and checked for collision - if collision is present, we loop through globes here
if (nextLine && nextLine.stage) {
addChild(nextLine);
}
//check for active globes
if (lines[i].visible && lines[i].hitTestObject(globe)) {
//check if the selected line touches the selected globe - if it does then we will start the timer for that globe
if (!globe.running){
globe.start();
//trace('timing');
kill = false;
}
}
else {
globe.reset();
}
switch(mirror.currentFrame) {
case 1:
break;
case 2:
//trace('live a life you will remember' + Math.random());
if(nextLine) nextLine.visible = true;
lines[i].draw(mirror.x + (mirror.width / 2),lines[i].curY);
if (nextLine) {
nextLine.curX = mirror.x + (mirror.width / 2);
nextLine.curY = 200;
nextLine.draw(mirror.x + (mirror.width / 2), 0);
}
kill = false;
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
trace(mirror.currentFrame);
kill = false;
break;
}
}
}
else if (lines[i].visible && mirror.stage && lines[i].stage){
if (kill && nextLine){
nextLine.graphics.clear();
nextLine.visible = false;
}
}
}
}
}
}
}
}
//MIRROR CLASS DECLARATION
import flash.events.MouseEvent;
class Mirror extends MovieClip {
trace('mirror: working');
public function Mirror() {
this.addEventListener(MouseEvent.MOUSE_DOWN,onDown,false,0,true);
}
private function onDown(e:MouseEvent):void {
//add the mouse up listener on the stage, that way it's consistent even if the user drags so fast that the mouse leaves the bounds of the mirror
stage.addEventListener(MouseEvent.MOUSE_UP, onUp, false, 0, true);
this.startDrag();
}
private function onUp(e:MouseEvent):void {
//we need to remove the listener from the stage now
stage.removeEventListener(MouseEvent.MOUSE_UP, onUp, false);
this.stopDrag();
}
}
//LINE CLASS DECLARATION
import flash.display.Graphics;
class Line extends MovieClip {
trace('line: working');
public var curX:int;
public var curY:int;
public function Line():void {
}
public function draw(toX:int,toY:int):void {
graphics.clear();
graphics.lineStyle(1,0x000000,1);
graphics.moveTo(curX,curY);
graphics.lineTo(toX, toY);
curX = toX;
curY = toY;
}
}
//GLOBE CLASS DECLARATION
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
class Globe extends MovieClip {
trace('globe: working');
private var timer:Timer = new Timer(3 * 100, 5);
public function Globe():void {
timer = new Timer(300, 5);
timer.addEventListener(TimerEvent.TIMER, repeatShine, false, 0, true);
}
public function reset():void {
timer.reset();
}
public function start():void {
timer.start();
}
public function get running():Boolean { return timer.running; };
private function repeatShine(e:TimerEvent):void {
}
}

Flash events execution order

Check out the next code, tell me what do you expect to be printed. Then run it and check what is really happen.
package
{
import flash.display.Sprite;
import flash.events.Event;
public class TestFlashEvents extends Sprite
{
private static const DUMMY_EVENT:String = "DummyEvent";
private var dummyObjects:Vector.<DummyObject> = new Vector.<DummyObject>(100);
public function TestFlashEvents()
{
for(var i:int = 0; i < dummyObjects.length; i++){
dummyObjects[i] = new DummyObject(this);
addEventListener(DUMMY_EVENT, dummyObjects[i].listener);
}
removeEventListener(DUMMY_EVENT, dummyObjects[41].listener);
dispatchEvent(new Event(DUMMY_EVENT));
}
private var counter:int = 0;
public function onGettingEvent(dummyObject:DummyObject):void{
if(counter == 25){
for(var i:int = 0; i < 50; i++){
removeEventListener(DUMMY_EVENT, dummyObjects[i].listener);
trace("Removing", dummyObjects[i].id);
}
}
trace("Handeling event", counter, dummyObject.id);
counter++;
}
}
}
import flash.events.Event;
class DummyObject
{
private static var dummyObjectsCounter:int = 0;
public var id:String;
private var tester:TestFlashEvents;
public function DummyObject(tester:TestFlashEvents)
{
this.tester = tester;
id = "DummyObject " + dummyObjectsCounter;
dummyObjectsCounter++;
}
public function listener(event:Event):void{
tester.onGettingEvent(this);
}
}
The removeEventListener function is actually not working. Tell me what do you think about it. I also open a bug in adobe.
This code means Adobe caches event listener list prior to actually calling event listeners. It is an unusual behavior to have two listeners for one particular event over one single object, but if it happens, Adobe assumed that all listeners should be invoked prior to actually modifying this list. I was actually expecting all 99 listeners to get called. So, this behavior can even be by design, because re-rendering the event listener list while processing a single event will put a too heavy load on Flash, so that the lags will galore. No one wants lags.
This can easily be fixed by prioritizing you event listeners.
This is not a Flash bug, it is, like Vesper said caching of event listeners to prevent lag.
Here is the updated version of your code, mark the prioritizing on the addEventListener call.
package
{
import flash.display.Sprite;
import flash.events.Event;
public class TestFlashEvents extends Sprite
{
private var _this = this;
private static const DUMMY_EVENT:String = "DummyEvent";
private var dummyObjects:Vector.<DummyObject> = new Vector.<DummyObject>(100);
public function TestFlashEvents()
{
for(var i:int = 0; i < dummyObjects.length; i++)
{
dummyObjects[i] = new DummyObject(this);
addEventListener(DUMMY_EVENT, dummyObjects[i].listener, false, dummyObjects.length - i);
}
removeEventListener(DUMMY_EVENT, dummyObjects[41].listener);
dispatchEvent(new Event(DUMMY_EVENT));
}
private var counter:int = 0;
public function onGettingEvent(dummyObject:DummyObject):void
{
if (counter == 25)
{
for (var i:int = 0; i < 50; i++)
{
removeEventListener(DUMMY_EVENT, dummyObjects[i].listener);
trace("Removing", dummyObjects[i].id);
}
}
trace("Handeling event", counter, dummyObject.id);
counter++;
}
}
}
import flash.events.Event;
class DummyObject
{
private static var dummyObjectsCounter:int = 0;
public var id:String;
private var tester:TestFlashEvents;
public function DummyObject(tester:TestFlashEvents)
{
this.tester = tester;
id = "DummyObject " + dummyObjectsCounter;
dummyObjectsCounter++;
}
public function listener(event:Event):void{
tester.onGettingEvent(this);
}
}

AS3 Object Array not adding to stage

So I am new to AS3 and AS in general. I do know Java to a degree and PHP very well.
I am trying to learn by writing an app that creates a hex map programatically. I am using a library for hexagons which I will include below however, here is my issue.
When I run a for loop for say, a 10 count, and I then create a shape, add the shape and draw the shape, it has no issue. When in the same loop if I push a shape to an array, then add that array element, then draw it, I get nothing.
package
{
import flash.display.MovieClip;
import flash.display.Shape;
import flash.text.TextField;
public class Main extends MovieClip
{
private var radius:Number = 30;
private var sides:Number = 6;
private var myrotation:Number = 0;
private var lineColor:Number = 0x000000;
private var lineThickness:Number = 1;
private var l:TextField = new TextField();
private var f:Array = new Array(20);
public function Main()
{
l.text = "test";
addChild(l);
//WORKS should be red
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++)
{
var t:Polygon = new Polygon();
addChild(t);
t.drawPolygon(radius,sides,2*radius*j,radius*2,0xFF0000,lineThickness,myrotation);
}
//DOES NOT WORK (note the *3 is so it is lower down) should be blue
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++)
{
f.push(new Polygon());
addChild(f[j]);
f[j].drawPolygon(radius,sides,2*radius*j,radius*3,0x0000FF,lineThickness,myrotation);
}
}
}
}
The polygon class I am using is below. I grabbed it off the internets somewhere.
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Polygon extends MovieClip
{
//PROPERTIES
private var points:Array;
private var id:int;
private var ratio:Number;
private var top:Number;
private var fade_value:int;
//CONSTRUCTOR
public function Polygon()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
stage.frameRate=31;
}
//METHODS
public function drawPolygon(radius:int,segments:int,centerX:Number,centerY:Number,tint:uint,line:int,rotating:Number):void
{
id=0;
points=new Array();
ratio=360/segments;
top=centerY-radius;
for(var i:int=rotating;i<=360+rotating;i+=ratio)
{
var xx:Number=centerX+Math.sin(radians(i))*radius;
var yy:Number=top+(radius-Math.cos(radians(i))*radius);
points[id]=new Array(xx,yy);
if(id>=1)
{
drawing(points[id-1][0],points[id-1][1],points[id][0],points[id][1],tint,line);
}
id++;
}
id=0;
}
private function radians(n:Number):Number
{
return(Math.PI/180*n);
}
private function drawing(startX:Number,startY:Number,stopX:Number,stopY:Number,tint:Number,line:int):void
{
graphics.lineStyle(line,tint,1);
graphics.moveTo(startX,startY);
graphics.lineTo(stopX,stopY);
}
public function fadeOut():void
{
fade_value=0;
addEventListener(Event.ENTER_FRAME,fade);
}
public function fadeIn():void
{
fade_value=1;
addEventListener(Event.ENTER_FRAME,fade);
}
//PRIVATE
private function fade(evt:Event):void
{
var da:Number=fade_value-alpha;
var aa:Number=da*.1;
alpha+=aa;
if(Math.abs(da)<=.05)
{
alpha=fade_value;
removeEventListener(Event.ENTER_FRAME,fade);
if(fade_value==0)
dispatchEvent(new Event('fadeOutDone'));
else
dispatchEvent(new Event('fadeInDone'));
}
}
}
}
----EDIT----
the code is now as follows with the same issues occurring. I've tried adding a vector and i tried casting the array element.
package
{
import flash.display.MovieClip;
import flash.display.Shape;
import flash.text.TextField;
public class Main extends MovieClip
{
private var radius:Number = 30;
private var sides:Number = 6;
private var myrotation:Number = 0;
private var lineColor:Number = 0x000000;
private var lineThickness:Number = 1;
private var l:TextField = new TextField();
private var f:Array = new Array(20);
private var v:Vector.<Polygon> = new Vector.<Polygon>(10);
public function Main()
{
l.text = "test";
addChild(l);
//WORKS should be red
for(var i:int = 0; i<stage.stageWidth/(radius*2);i++)
{
var t:Polygon = new Polygon();
addChild(t);
t.drawPolygon(radius,sides,2*radius*i,radius*2,0xFF0000,lineThickness,myrotation);
}
//DOES NOT WORK (note the *3 is so it is lower down) should be blue
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++)
{
f.push(new Polygon());
addChild(f[j]);
Polygon(f[j]).drawPolygon(radius,sides,2*radius*j,radius*3,0x0000FF,lineThickness,myrotation);
}
for(var k:int = 0; k<stage.stageWidth/(radius*2);k++)
{
v.push(new Polygon());
addChild(v[k]);
v[k].drawPolygon(radius,sides,2*radius*k,radius*3.5,0x00FF00,lineThickness,myrotation);
}
}
}
}
compile notes...
Loading configuration file C:\Program Files (x86)\Adobe\Adobe Flash Builder 4.6\
sdks\4.6.0\frameworks\flex-config.xml
Initial setup: 64ms
start loading swcs 14ms Running Total: 78ms
Loaded 30 SWCs: 484ms
precompile: 841ms
C:\Users\jmasiello.place\Documents\flash\Main.as: Warning: This compila
tion unit did not have a factoryClass specified in Frame metadata to load the co
nfigured runtime shared libraries. To compile without runtime shared libraries e
ither set the -static-link-runtime-shared-libraries option to true or remove the
-runtime-shared-libraries option.
Files: 145 Time: 961ms
Linking... 25ms
Optimizing... 26ms
SWF Encoding... 11ms
C:\Users\jmasiello.place\Documents\flash\Main.swf (1809 bytes)
postcompile: 65ms
Total time: 1595ms
Peak memory usage: 47 MB (Heap: 29, Non-Heap: 18)
WWhen you do
private var f:Array = new Array(20);
You create an array with 20 elements in implements
Then later on you are pushing an element into the same array.
f.push(new Polygon());
By pushing an element you are adding to the last element
Which makes the array have the first 20 elements null and the 21st element be a Polygon
You can even verify it by doing.
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++){
f.push(new Polygon());
trace(f)
}
So in your code you are pushing into the 21st element and then accessing the array via
addChild(f[j]);
Which j = 0 and the element in 0 is still null
Then best way to fix it would be to
private var f:Array = new Array();
Are you getting any errors? You probably need to cast to Polygon in order for the compiler to accept the drawPolygon method.
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++) {
f.push(new Polygon());
addChild(f[j]);
Polygon(f[j]).drawPolygon(radius,sides,2*radius*j,radius*3,0x0000FF,lineThickness,myrotation;
}
To skip the casting you can use a Vector instead of an Array as then the compiler will know that it handles Polygons exclusively.

How to make new bubbles stack in my Bubble Shooter game?

I'm making a Bubble Shooter game, and I'm trying to make the bubble I'm fireing to stack and then be at the right place in the column. The bubbles I've placed on the board looks like this:
000000000000000
000000000000000
000000000000000
000000000000000
There's 4 rows with 15 bubbles. This is the code I have written so far:
Main
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.SpreadMethod;
public class Main extends Sprite {
private const ROT_SPEED:uint=2;
private const R:uint=18;
private const DEG_TO_RAD:Number=0.0174532925;
private const BUBBLE_SPEED:uint=10;
private var bubbleArr:Array=new Array();
private var loadArr:Array=new Array();
private var cannon:cannon_mc;
private var bubble:bubble_mc;
private var row:uint=0;
private var col:uint=0;
private var left:Boolean=false;
private var right:Boolean=false;
public var bubCont:Sprite;
private var loadCont:Sprite;
private var fire:Boolean=false;
private var vx,vy:Number;
public function Main() {
placeContainer();
placeCannon();
loadBubble();
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onKUp);
addEventListener(Event.ENTER_FRAME,onEFrame);
trace("row= "+row+" , col= "+col);
}
private function placeCannon():void {
cannon=new cannon_mc();
addChild(cannon);
cannon.y=385.5;
cannon.x=320;
}
private function onKDown(e:KeyboardEvent):void {
switch(e.keyCode) {
case 37 :
left=true;
break;
case 39 :
right=true;
break;
case 38 :
if (! fire) {
fire=true;
var radians=(cannon.rotation-90)*DEG_TO_RAD;
vx=BUBBLE_SPEED*Math.cos(radians);
vy=BUBBLE_SPEED*Math.sin(radians);
}
break;
}
}
private function onKUp(e:KeyboardEvent):void {
switch(e.keyCode) {
case 37 :
left=false;
break;
case 39 :
right=false;
break;
}
}
private function onEFrame(e:Event):void {
if (left) {
cannon.rotation-=ROT_SPEED;
}
if (right) {
cannon.rotation+=ROT_SPEED;
}
if (fire) {
bubble.x+=vx;
bubble.y+=vy;
if (bubble.x<59) {
bubble.x=59;
vx*=-1;
}
if (bubble.x>(59+R*R)) {
bubble.x=59+R*R;
vx*=-1;
}
if (bubble.y<(40)) {
bubble.y=40;
}
}
}
public function placeContainer():void {
var iRow:Boolean=false;
bubCont=new Sprite();
addChild(bubCont);
for (var i:uint=0; i<4; i++) {
if (! iRow) {
for (var j:uint=0; j<15; j++) {
bubbleArr[i]=new Array();
bubbleArr[i][j]=Math.floor(Math.random()*6);
bubble = new bubble_mc(bubbleArr[i][j],i,j);
bubCont.addChild(bubble);
iRow=true;
row++;
col++;
}
} else {
for (j=0; j<15; j++) {
bubbleArr[i]=new Array();
bubbleArr[i][j]=Math.floor(Math.random()*6);
bubble = new bubble_mc(bubbleArr[i][j],i,j);
bubble.x=77+j*2*R;
bubCont.addChild(bubble);
iRow=false;
row++;
col++;
}
}
}
}
private function loadBubble():void {
addChild(bubble);
bubble.gotoAndStop(Math.floor(Math.random()*6))+1;
bubble.x=320;
bubble.y=410;
}
}
bubble_mc class:
package {
import flash.display.MovieClip;
public class bubble_mc extends MovieClip {
public function bubble_mc(val:uint,row:uint,col:uint) {
gotoAndStop(val+1);
name=row+"_"+col;
x=59+col*36;
y=40+row*32;
}
}
I have absolutley no idea how to make the bubbles stack together.. I have tried using hitTestObject-function and I have tried to write my own function that checks for collision and then calls a function that is supposed to place the bubble in the right place, but it doesn't work and I dont know why. I'm getting a error called TypeError: Error #1010.
Here is the collision function and the parkBubble function- which is supposed to place the bubbles in the right place:
private function parkBubble(bubble:bubble_mc,row:int,col:int):void {
var iRow:Boolean=false;
for (var j:uint=0; j<col; j++) {
trace("first for loop ");
for (var i:uint=row; i>0; i--) {
trace("second for loop ");
if (bubbleArr[i][j]!=null) {
trace("first if loop ");
if (! iRow) {
trace("second if loop ");
bubbleArr[i+1]=new Array();
bubbleArr[i+1][j]=Math.floor(Math.random()*6);
bubble = new bubble_mc(bubbleArr[i+1][j],(i+1),j);
bubCont.addChild(bubble);
iRow=true;
row++;
col++;
} else {
trace("first for loop after else ");
bubbleArr[i+1]=new Array();
bubbleArr[i+1][j]=Math.floor(Math.random()*6);
bubble = new bubble_mc(bubbleArr[i+1][j],(i+1),j);
bubble.x=77+j*2*R;
bubCont.addChild(bubble);
iRow=false;
row++;
col++;
}
}
}
}
removeChild(bubble);
fire=false;
loadBubble();
trace("slutet av parkBubble ");
}
private function collide(bub:bubble_mc):Boolean {
var dist_x:Number=bub.x-bubble.x;
var dist_y:Number=bub.y-bubble.y;
return Math.sqrt(dist_x*dist_x+dist_y*dist_y)<=2*R-4;
}
Was the TypeError on this line?
var placed_bubble:bubble_mc=new bubble_mc([row][col],row,col);
The [row] is an array, and [col] is an array. But the constructor expects an unsigned integer:
public function bubble_mc(val:uint,row:uint,col:uint) {
In order to copy the bubble to the bubble container, pass the frame number:
var placed_bubble:bubble_mc=new bubble_mc(bubble.currentFrame-1, row, col);
This might not be the only problem. TypeError often results from a variable not being defined, which could be from some other code that modifies the variable "bubble". For example, placeContainer assigns the bubbles in the container to the variable "bubble".
The function parkBubble always sets "iRow" to false, but if the bubble collides with a row above it that is odd you want iRow to be true.
var row:uint=Math.floor(bubble.y/(40+R*Math.sqrt(3)));
var iRow:Boolean= row % 2 == 1 ? true : false;
After it is at least compiling, you'll have less problems if you go back and simplify and optimize the math with some constant names. Then you'll more easily see the above code for calculating the row is not quite right. It should subtract the top margin (40). That is obvious with named constants:
private const Y_PER_ROW:int = int(R * Math.sqrt(3));
private const TOP:int = 40;
...
var row:uint = int((bubble.y - TOP) / Y_PER_ROW);
I would double-check your other calculations, too. Puzzle Bobble games usually set the odd rows to horizontally offset at radius, not at 2 radius (2 * R). So they fit together like hexagons.
The placeContainer function could be simplified. Most of the code in even or odd rows is the same, so could be taken out of the if block. And in this code you posted, I don't see why you need "row++" and "col++" in placeContainer. This is equivalent and easier to read:
for (var i:uint=0; i<4; i++) {
var xOffset:int = (i % 2) * 2 * R;
for (var j:uint=0; j<15; j++) {
bubbleArr[i] = new Array();
bubbleArr[i][j] = int(Math.random()*6);
bubble = new bubble_mc(bubbleArr[i][j], i, j);
bubble.x += xOffset;
bubCont.addChild(bubble);
row++;
col++;
}
}
Then, the collision detection code could be simplified and optimized to avoid calculations when the bubble is far away and avoid the expensive square-root calculation:
Circle Collision Detection HTML5 Canvas
http://cgp.wikidot.com/circle-to-circle-collision-detection

AS3 Creating an array of objects

I would like to add a bunch of cars to the stage, and store them in an array as objects. The problem is I hate using external AS files and would like to keep it as simple as possible.
I tried doing :
var car:Object = {carcolor:String,carscale:Number,carpower:Number};
var test:Array = new Array()
for (var i:Number=0; i<10; i++) {
test.push(car)
}
The problem is if I try to set a value of one object in the like
test[1].carscale = 5
Every object in the array gets their attribute carscale set to 5.
Is there any way I can do this without using external class files?
While you should use external AS files (its a good practice), here's the reason why you are having the issue, and I'm going to explain line-by-line
var car:Object = {carcolor:String,carscale:Number,carpower:Number};
//This creates an object called car. Suppose it saves it in memory at "location" 0x12345
var test:Array = new Array();
//This creates an empty array
for (var i:Number=0; i<10; i++) {
test.push(car);
//This adds the object "car" to the array
//Since Object is a reference type, its memory location is actually added to the array
//This means you added 0x12345 to the array (10 times over the loop)
}
//The array now contains
[0x12345, 0x12345, 0x12345, .......];
//So now
test[1]; //returns the object at 0x12345
test[1].carscale=5; //sets the carscale property of the object at 0x12345
Since all objects in the array point to the same location, getting any of them will actually return the same object. This means that all of them will show carscale as 5
A solution to this would be:
var test:Array = new Array();
for (var i:Number=0; i<10; i++) {
var car:Object = {carcolor:String,carscale:Number,carpower:Number};
test.push(car);
}
A better, REAL Object oriented solution would be to create a class called Car and then instead of doing
var car:Object = {carcolor:String,carscale:Number,carpower:Number};
you use
var car:Car = new Car();
The Car.as class would be like this:
public class Car {
public function Car() {
//this is the constructor, initialize the object here
//Suppose the default values of the car are as follows:
carcolor="red";
carscale=5;
carpower=1000;
}
public var carcolor:String;
public var carscale:Number, carpower:Number;
}
In fact, you could even use another constructor that automatically sets the properties based on arguments:
public function Car(_color:String, _scale:Number, _power:Number) {
carcolor=_color;
carscale=_scale;
carpower=_power;
}
and call it as
var car:Car=new Car("red", 5, 1000);
In fact, the car before carcolor, carscale and carpower is not even necessary because it is obvious when you put them in a class called Car.
Like TheDarkIn1978 said you're pushing a reference of your car instance into your array. When you change the value of one instance's property the same happens for each reference.
The simple answer is to create a new object upon each interation of your for loop like in the following:
var test:Array = [];
for (var i:Number = 0; i < 10; i++)
{
var car:Object = {carcolor:String, carscale:Number, carpower:Number};
test.push(car);
}// end for
[UPDATE]
I know you said that you didn't want to use "external classes" but there are advantages to using a custom class object to store values as opposed to a Object object. Here is an example:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var cars:Vector.<Car> = new Vector.<Car>();
cars.push(new Car("red", 1, 1));
cars.push(new Car("blue", 2, 2));
cars.push(new Car("green", 3, 3));
trace(cars[2].color); // output: green
}// end function
}// class
}// end package
internal class Car
{
private var _color:String;
private var _scale:Number;
private var _power:Number;
public function get color():String { return color; }
public function get scale():String { return scale; }
public function get power():String { return power; }
public function Car(color:String, scale:Number, power:Number)
{
_color = color;
_scale = scale;
_power = power;
}// end function
}// end class
This is a good example of creating an object for the sole purpose of storing values that never change by only allowing the object's properties to be set upon initiation and using getter methods to make the values read only.
I feel dumb, I found the answer here :
http://board.flashkit.com/board/showthread.php?t=792345
You're pushing the Object reference to the array, not a unique Object each time. You have to do something like:
for(var temp=0;temp<100;temp++){
var roomData:Object=new Object;
roomData.first_time=true;
rooms.push(roomData);
}
you're adding the same object to the array multiple times. you need to create new instances of your car object.
EDIT:
although it would be a best practice to create your own "Car" class and create new instances of it, even if it's only a small object with 3 properties, here's a quick example that should get you started.
package
{
//Imports
import flash.display.Sprite;
//Class
public class Main extends Sprite
{
//Constants
private static const DEFAULT_CAR_COLOR:Number = 0x000000;
private static const DEFAULT_CAR_SCALE:Number = 1.0;
private static const DEFAULT_CAR_POWER:int = 50;
//Properties
private var carsArray:Array;
//Constructor
public function Main():void
{
init();
outputCarColors();
}
//Initialize
private function init():void
{
carsArray = new Array();
for (var i:int = 0; i < 10; i++)
{
carsArray.push(CreateCar(Math.random() * 0xFFFFFF));
}
}
//Output Car Colors
private function outputCarColors():void
{
for (var i:int = 0; i < carsArray.length; i++)
{
trace("Color of car " + i + " : " + carsArray[i].carColor);
}
}
//Create Car Object
private function CreateCar(carColor:Number = DEFAULT_CAR_COLOR, carScale:Number = DEFAULT_CAR_SCALE, carPower:int = DEFAULT_CAR_POWER):Object
{
var result:Object = new Object();
result.carColor = carColor;
result.carScale = carScale;
result.carPower = carPower;
return result;
}
}
}