ActionScript 3 : Particle Collition - actionscript-3

I'm working on my school assignment and i got stuck.
So, the idea is i want to make somehing like in this video, particle collision demonstration (https://www.youtube.com/watch?v=wKqpOOgXLzE) but i cant find any tutorial for that. So i tried to find a tutorial that has similar concept with the video, the pong game. I found a tutorial with multiple ball colliding each other (https://www.emanueleferonato.com/2008/06/07/managing-multiple-balls-collisions-with-flash-as3-version/). and i tried to modify the code to make the ball hit the platform, but sadly it did nothing and i cant think of any other solution.
here's the code:
public class BallCollision extends MovieClip
{
private var mcBallContainer:MovieClip;
private var nNumBalls:Number = 15;
private var mcBrickCOntainer:MovieClip;
private var nStageWidth:Number = 200;
private var nStageHeight:Number = 200;
private var brick1: Brick;
var score:Number = 0;
public function BallCollision ( ) : void
{
mcBallContainer = new MovieClip ( ) ;
mcBallContainer.x = 755;
mcBallContainer.y = 408;
stage.addChild(mcBallContainer);
mcBrickCOntainer = new MovieClip();
mcBrickCOntainer.x = 200;
mcBrickCOntainer.y = 250;
stage.addChild(mcBrickCOntainer);
this.addEventListener ( Event.ENTER_FRAME, enterFrameHandler );
for ( var i = 0; i < nNumBalls; i++ )
{
var mcBall:Ball = new Ball ( Math.floor ( ( Math.random() * 12 ) - 4 ), Math.floor ( ( Math.random() * 12 ) - 4 ) );
mcBall.x = Math.random() * nStageWidth;
mcBall.y = Math.random() * nStageHeight;
mcBallContainer.addChild ( mcBall );
brick1 = new Brick();
brick1.x = 700;
brick1.y = 350;
mcBrickCOntainer.addChild(brick1);
}
}
private function enterFrameHandler ( E:Event ) : void
{
for ( var i = 0; i < mcBallContainer.numChildren; i++ )
{
var mcBall1:* = mcBallContainer.getChildAt( i );
for ( var j = i + 1; j < mcBallContainer.numChildren; j++ )
{
var mcBall2:* = mcBallContainer.getChildAt( j );
var nDistX:Number = Math.abs ( mcBall1.x - mcBall2.x );
var nDistY:Number = Math.abs ( mcBall1.y - mcBall2.y );
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
if ( nDistance < 20 )
{
solveBalls ( mcBall1, mcBall2 );
score += 1;
trace(score);
}
}
}
scoreCounter.text = String(score);
//brickCalling();
}
/*public function brickCalling(){
}*/
private function solveBalls ( MCBallA:MovieClip, MCBallB:MovieClip) : void
{
var nX1:Number = MCBallA.x;
var nY1:Number = MCBallA.y;
var nDistX:Number = MCBallB.x - nX1;
var nDistY:Number = MCBallB.y - nY1;
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
var nRadiusA:Number = MCBallA.width/2;
var nRadiusB:Number = MCBallB.width/2;
//var nRadius:Number = 10;
var nNormalX:Number = nDistX/nDistance;
var nNormalY:Number = nDistY/nDistance;
var nMidpointX:Number = ( nX1 + MCBallB.x )/2;
var nMidpointY:Number = ( nY1 + MCBallB.y )/2;
MCBallA.x = nMidpointX - nNormalX * nRadiusA;
MCBallA.y = nMidpointY - nNormalY * nRadiusA;
MCBallB.x = nMidpointX + nNormalX * nRadiusB;
MCBallB.y = nMidpointY + nNormalY * nRadiusB;
var nVector:Number = ( ( MCBallA.nSpeedX - MCBallB.nSpeedX ) * nNormalX )+ ( ( MCBallA.nSpeedY - MCBallB.nSpeedY ) * nNormalY );
var nVelX:Number = nVector * nNormalX;
var nVelY:Number = nVector * nNormalY;
MCBallA.nSpeedX -= nVelX;
MCBallA.nSpeedY -= nVelY;
MCBallB.nSpeedX += nVelX;
MCBallB.nSpeedY += nVelY;
}
}
}
I hope someone can guide me, Thanks!

Have you considered using a physics engine, like box2D?
https://www.box2dflash.org

Related

How can I use this code on frame

I trying to make bouncing balls.I use this example:
But I don't work on class files, and I'm trying to use as frame code.Here is what I do so far.
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import Ball;
var nSpeedX:Number;
var nSpeedY:Number;
var nStageWidth:Number = 500;
var nStageHeight:Number = 400;
var NSpeedX:Number;
var NSpeedY:Number;
var mcBallContainer:MovieClip;
var MCBallA:MovieClip;
var MCBallB:MovieClip;
var nNumBalls:Number = 1;
stage.addEventListener(Event.ENTER_FRAME,BallCollision);
function BallCollision(oly:Event) {
mcBallContainer = new MovieClip ( ) ;
mcBallContainer.x = mcBallContainer.y = 0;
stage.addChild(mcBallContainer);
this.addEventListener ( Event.ENTER_FRAME, enterFrameHandler );
for (var i = 0; i < nNumBalls; i++)
{
var mcBall:Ball = new Ball ( Math.floor ( ( Math.random() * 1 ) - 4 ), Math.floor ( ( Math.random() * 12 ) - 4 ) );
mcBall.x = Math.random() * nStageWidth;
mcBall.y = Math.random() * nStageHeight;
mcBallContainer.addChild ( mcBall );
}
stage.removeEventListener(Event.ENTER_FRAME,BallCollision) //Fixed
}
function enterFrameHandler(oly:Event) {
for ( var i = 0; i < mcBallContainer.numChildren; i++ )
{
var mcBall1:* = mcBallContainer.getChildAt( i );
for ( var j = i + 1; j < mcBallContainer.numChildren; j++ )
{
var mcBall2:* = mcBallContainer.getChildAt( j );
var nDistX:Number = Math.abs ( mcBall1.x - mcBall2.x );
var nDistY:Number = Math.abs ( mcBall1.y - mcBall2.y );
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
if ( nDistance < 20 )
{
solveBalls ( mcBall1, mcBall2 );
}
}
}
//
function solveBalls ( MCBallA:MovieClip, MCBallB:MovieClip ) : void{
var nX1:Number = MCBallA.x;
var nY1:Number = MCBallA.y;
var nDistX:Number = MCBallB.x - nX1;
var nDistY:Number = MCBallB.y - nY1;
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
var nRadiusA:Number = MCBallA.width/2;
var nRadiusB:Number = MCBallB.width/2;
//var nRadius:Number = 10;
var nNormalX:Number = nDistX/nDistance;
var nNormalY:Number = nDistY/nDistance;
var nMidpointX:Number = ( nX1 + MCBallB.x )/2;
var nMidpointY:Number = ( nY1 + MCBallB.y )/2;
MCBallA.x = nMidpointX - nNormalX * nRadiusA;
MCBallA.y = nMidpointY - nNormalY * nRadiusA;
MCBallB.x = nMidpointX + nNormalX * nRadiusB;
MCBallB.y = nMidpointY + nNormalY * nRadiusB;
var nVector:Number = ( ( MCBallA.nSpeedX - MCBallB.nSpeedX ) * nNormalX )+ ( ( MCBallA.nSpeedY - MCBallB.nSpeedY ) * nNormalY );
var nVelX:Number = nVector * nNormalX;
var nVelY:Number = nVector * nNormalY;
MCBallA.nSpeedX -= nVelX;
MCBallA.nSpeedY -= nVelY;
MCBallB.nSpeedX += nVelX;
MCBallB.nSpeedY += nVelY;
}
}
My code is working but balls number More than it should be.And balls is not collision each other. What is wrong of my code ??
UPDATED Thanks #Organis

Blank Maze Generation

Maze is generating in a weird fashion. I do not know why, but it generates and breaks down every wall.
here is the code:
public class Main extends Sprite
{
//ARRAYS
private var maze:Array = new Array();
private var pos:Array = new Array();
//INTEGERS
private var num:int = 50;
private var cellX:int = 0;
private var cellY:int = 0;
private var mazeX:int = num;
private var mazeY:int = num;
//POINTS
private var startCell:Point = new Point();
//SPRITES
private var mazeSpriteT:Sprite = new Sprite();
private var mazeSpriteB:Sprite = new Sprite();
private var mazeSpriteL:Sprite = new Sprite();
private var mazeSpriteR:Sprite = new Sprite();
//broken wall
private var brWall:Sprite = new Sprite();
public function Main():void
{
//creating the initial grid, cells and points
setMaze();
//calling function to generate maze
generate(startCell);
}
public function setMaze():void
{
//clearing the screen from all graphical images
mazeSpriteT.graphics.clear();
mazeSpriteB.graphics.clear();
mazeSpriteR.graphics.clear();
mazeSpriteL.graphics.clear();
brWall.graphics.clear();
maze = new Array();
pos = new Array();
for (var i:int = 0; i < 10; i++) {
maze[i] = new Array();
pos[i] = new Array();
for (var j:int = 0; j < 10; j++) {
//maze
maze[i][j] = new Cell;
maze[i][j].visited = false;
//pos
pos[i][j] = new Point(cellX, cellY);
//graphics
mazeSpriteT.graphics.lineStyle(5, 0x000000, 1);
mazeSpriteB.graphics.lineStyle(5, 0x000000, 1);
mazeSpriteR.graphics.lineStyle(5, 0x000000, 1);
mazeSpriteL.graphics.lineStyle(5, 0x000000, 1);
//toplines
addChild(mazeSpriteT);
mazeSpriteT.graphics.moveTo(mazeX, mazeY);
mazeSpriteT.graphics.lineTo(mazeX + num, mazeY);
mazeSpriteT = new Sprite();
//bottomlines
addChild(mazeSpriteB);
mazeSpriteB.graphics.moveTo(mazeX, mazeY + num);
mazeSpriteB.graphics.lineTo(mazeX + num, mazeY + num);
mazeSpriteB = new Sprite();
//rightlines
addChild(mazeSpriteR);
mazeSpriteR.graphics.moveTo(mazeX + num, mazeY);
mazeSpriteR.graphics.lineTo(mazeX + num, mazeY + num);
mazeSpriteR = new Sprite();
//leftlines
addChild(mazeSpriteL);
mazeSpriteL.graphics.moveTo(mazeX, mazeY);
mazeSpriteL.graphics.lineTo(mazeX, mazeY + num);
mazeSpriteB = new Sprite();
cellX += 10;
mazeX += num;
}
cellX = 0;
cellY += 10;
mazeX = num;
mazeY += num;
}
//maze entrance
startCell = pos[0][0];
}
public function generate(cell:Point):void
{
var cx:int = cell.x / 10;
var cy:int = cell.y / 10;
maze[cy][cx].visited = true;
var neighbours:Array = new Array();
fillNeighbours(neighbours, cell);
trace(neighbours.length);
while (neighbours.length > 0) {
var index:int = (Math.random() * neighbours.length);
Math.floor(index);
var arr:Array = neighbours.splice(index, 1);
var pnt:Point = new Point(arr[0].x, arr[0].y);
breakWall(cell, pnt);
generate(pnt);
//trace("index: " + index);
//trace("obx: " + arr[0].x + " oby: " + arr[0].y);
//trace("pnt: " + pnt);
}
}
public function fillNeighbours(neighbours:Array, cell:Point):Array
{
var cx:int = cell.x / 10;
var cy:int = cell.y / 10;
/*for (var i:int = 0; i < maze.length; i++) {
for (var j:int = 0; j < maze[i].length; j++) {
//outerwall parameters
maze[0][j].north = false;
maze[maze.length - 1][j].south = false;
maze[i][maze[i].length-1].east = false;
maze[i][0].west = false;
}
} */
//south neigbours
if (cy < maze.length - 1) {
if (maze[cy + 1][cx].visited == true) {
maze[cy][cx].south = false;
} else {
neighbours.push(pos[cy + 1][cx]);
}
}
if (cy > 0) {
//north neighbours
if (maze[cy - 1][cx].visited == true) {
maze[cy][cx].north = false;
} else {
neighbours.push(pos[cy - 1][cx]);
}
}
if (cx < maze.length - 1) {
//east neighbours
if (maze[cy][cx + 1].visited == true) {
maze[cy][cx].east = false;
} else {
neighbours.push(pos[cy][cx + 1]);
}
}
if (cx > 0) {
//west neighbours
if (maze[cy][cx - 1].visited == true) {
maze[cy][cx].west = false;
} else {
neighbours.push(pos[cy][cx - 1]);
}
}
trace("--");
return(neighbours);
}
public function breakWall(cell:Point, pnt:Point):void
{
var cx:int = cell.x / 10;
var cy:int = cell.y / 10;
var px:int = pnt.x / 10;
var py:int = pnt.y / 10;
brWall.graphics.lineStyle(5, 0xFFFFFF, 1); //white walls
addChild(brWall);
trace(cx, px);
if (cy == py) { //horizontal transition
if (cx > px) { //right to left
brWall.graphics.moveTo((cx * num) + num, (cy * num) + num);
brWall.graphics.lineTo((cx * num) + num, (cy * num) + (num * 2));
} else if (cx < px) { //left to right
brWall.graphics.moveTo((cx * num) + (num * 2), (cy * num) + (num * 2));
brWall.graphics.lineTo((cx * num) + (num * 2), (cy * num) + num);
}
}
if (cx == px) { //vertical transition
if (cy > py) { //down to up
brWall.graphics.moveTo((cx * num) + (num * 2), (cy * num) + num);
brWall.graphics.lineTo((cx * num) + num, (cy * num) + num);
} else if (cy < py){ //up to down
brWall.graphics.moveTo((cx * num) + num, (cy * num) + (num * 2));
brWall.graphics.lineTo((cx * num) + (num * 2), (cy * num) + (num * 2));
}
}
brWall = new Sprite();
}
}
}
I have been praying for help. Be the one to answer my prayers (I will give you five internet dollars).
I'm not too sure I understand completely. You're running a loop that breaks down each wall
while (neighbours.length > 0) {
var index:int = (Math.random() * neighbours.length);
Math.floor(index);
var arr:Array = neighbours.splice(index, 1);
var pnt:Point = new Point(arr[0].x, arr[0].y);
breakWall(cell, pnt);
generate(pnt);
//trace("index: " + index);
//trace("obx: " + arr[0].x + " oby: " + arr[0].y);
//trace("pnt: " + pnt);
}
Just remove breakWall(cell,pnt)?

Sprite layers are disappearing in AS3 drawing code

I'm building a custom painting application, and I'm running into a problem where the layers I'm adding to the canvas are disappearing after 24 layers get added.
Here is my code for the drawing tools:
//=====================================================================================================================================
// DRAWING FUNCTIONS
//-------------------------------------------------------------------------------------------------------------------------------------
protected function onMouseDown_canvas( evt:MouseEvent ):void
{
var p:Point = new Point( _realView.mouseX, _realView.mouseY );
_log.log( "CanvasViewController :: onMouseDown_canvas() - mouse:" + p );
_realView.addEventListener( MouseEvent.MOUSE_UP, onMouseUp_canvas );
_realView.addEventListener( MouseEvent.MOUSE_OUT, onMouseOut_canvas );
_realView.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove_canvas );
_currentLayer = new Sprite();
_currentLayer.mouseEnabled = false;
_realView.mcContainer.addChild( _currentLayer );
_$clearFirstBrushStroke = false;
switch( _$tool )
{
case ToolType.ERASER:
case ToolType.BRUSH:
case ToolType.PENCIL:
_currentLayer.graphics.beginFill(
( _$tool == ToolType.ERASER ? 0xffffff : _$color ),
( _$tool == ToolType.BRUSH ? .4 : 1 )
);
_currentLayer.graphics.drawCircle( p.x, p.y, _$toolSize/2 )
_currentLayer.graphics.endFill();
_firstPoint = p;
_$clearFirstBrushStroke = true;
if( _$tool == ToolType.BRUSH )
_currentLayer.filters = [ new BlurFilter( 3, 3, 2 ) ];
break;
case ToolType.BRUSH2:
drawGlitter( _currentLayer.graphics, p );
break;
}
}
//-------------------------------------------------------------------------------------------------------------------------------------
protected function onMouseUp_canvas( evt:MouseEvent ):void
{
var p:Point = new Point( _realView.mouseX, _realView.mouseY );
_log.log( "CanvasViewController :: onMouseUp_canvas() - mouse:" + p );
endDrawing();
}
//-------------------------------------------------------------------------------------------------------------------------------------
protected function onMouseMove_canvas( evt:MouseEvent ):void
{
var p:Point = new Point( _realView.mouseX, _realView.mouseY );
switch( _$tool )
{
case ToolType.ERASER:
case ToolType.BRUSH:
case ToolType.PENCIL:
if( _$clearFirstBrushStroke )
{
_$clearFirstBrushStroke = false;
_currentLayer.graphics.clear();
_currentLayer.graphics.lineStyle(
_$toolSize,
( _$tool == ToolType.ERASER ? 0xffffff : _$color ),
( _$tool == ToolType.BRUSH ? .4 : 1 ),
true,
LineScaleMode.NORMAL,
CapsStyle.ROUND,
JointStyle.ROUND
);
_currentLayer.graphics.moveTo( _firstPoint.x, _firstPoint.y );
}
_currentLayer.graphics.lineTo( p.x, p.y );
break;
case ToolType.BRUSH2:
drawGlitter( _currentLayer.graphics, p );
break;
}
}
//-------------------------------------------------------------------------------------------------------------------------------------
protected function onMouseOut_canvas( evt:MouseEvent ):void
{
var p:Point = new Point( _realView.mouseX, _realView.mouseY );
_log.log( "CanvasViewController :: onMouseOut_canvas() - mouse:" + p );
endDrawing();
}
//-------------------------------------------------------------------------------------------------------------------------------------
protected function endDrawing():void
{
var p:Point = new Point( _realView.mouseX, _realView.mouseY );
_log.log( "CanvasViewController :: endDrawing() - mouse:" + p );
_realView.removeEventListener( MouseEvent.MOUSE_UP, onMouseUp_canvas );
_realView.removeEventListener( MouseEvent.MOUSE_OUT, onMouseOut_canvas );
_realView.removeEventListener( MouseEvent.MOUSE_MOVE, onMouseMove_canvas );
var bmd:BitmapData = new BitmapData( _realView.width, _realView.height, true, 0x00000000 );
bmd.draw( _currentLayer );
var bmp:Bitmap = new Bitmap( bmd, "auto", true );
_realView.mcContainer.removeChild( _currentLayer );
_realView.mcContainer.addChild( bmp );
_currentLayer = null;
_log.log( " numChildren:" + _realView.mcContainer.numChildren );
}
//-------------------------------------------------------------------------------------------------------------------------------------
protected function drawGlitter( g:Graphics, c:Point, $glitterSize:Number = 2, $density:Number = .5 ):void
{
var $startAngle:Number = NewMath.Random( 0, Math.PI*2 );
var $stepAngle:Number = $startAngle / ( $density * _$toolSize );
for( var $i:int = 0; $i < ( $density * _$toolSize ); $i++ )
{
var $a:Number = $startAngle + ( $i * $stepAngle );
var $r:Number = NewMath.Random( 0, _$toolSize / 2 );
var p:Point = new Point( c.x + Math.cos( $a ) * $r, c.y + Math.sin( $a ) * $r );
g.beginFill( _$color, 1 );
g.drawCircle( p.x, p.y, $glitterSize / 2 );
g.endFill();
}
}
numChildren continues to grow, but once it hits 24 layers in that container, the first layer gets deleted or goes invisible or something. at 25 layers, the second layer disappears, etc.
See this in action here: http://dev.option5.net/CAUS/CAUS14002/index.html
Could it be a masking issue? Can only so many layers be masked : _realView.mcContainer (where new layers are added) is masked by _realView.mcMask?

Syntax error 1084 whatever i do

I am trying to make a tool for a game...
Pushing a button gives some results after calculations. The Code is not finished yet but it does not have to give errors. Here is the code:
import flash.events.MouseEvent;
//-----------------------variables------------------------
var iPPP1:String;
var iPPP2:String;
var iPPP3:String;
var iPPP4:String;
var iPPP5:String;
var iPPP6:String;
var iPPP7:String;
var iCntbonus:String;
var iRawPrice:String;
var iAverSal:String;
var iTax:String;
var iNoC7:String;
var iNoC6:String;
var iNoC5:String;
var iNoC4:String;
var iNoC3:String;
var iNoC2:String;
var iNoC1:String;
var iEm7:String;
var iEm6:String;
var iEm5:String;
var iEm4:String;
var iEm3:String;
var iEm2:String;
var iEm1:String;
var iRaw5:String;
var iRaw4:String;
var iRaw3:String;
var iRaw2:String;
var iRaw1:String;
var apotelesma:Number;
var bonus:Number;
var i1:Number;
var i2:Number;
var i3:Number;
var i4:Number;
var i5:Number;
var i6:Number;
var i7:Number;
//------------------------restricts------------------------
PPP1.restrict = "0-9\\.";
PPP2.restrict = "0-9\\.";
PPP3.restrict = "0-9\\.";
PPP4.restrict = "0-9\\.";
PPP5.restrict = "0-9\\.";
PPP6.restrict = "0-9\\.";
PPP7.restrict = "0-9\\.";
Cntbonus.restrict = "0-5";
RawPrice.restrict = "0-9\\.";
AverSal.restrict = "0-9\\.";
Tax.restrict = "0-9";
NoC7.restrict = "0-9";
NoC6.restrict = "0-9";
NoC5.restrict = "0-9";
NoC4.restrict = "0-9";
NoC3.restrict = "0-9";
NoC2.restrict = "0-9";
NoC1.restrict = "0-9";
Em7.restrict = "0-9";
Em6.restrict = "0-9";
Em5.restrict = "0-9";
Em4.restrict = "0-9";
Em3.restrict = "0-9";
Em2.restrict = "0-9";
Em1.restrict = "0-9";
Raw5.restrict = "0-9";
Raw4.restrict = "0-9";
Raw3.restrict = "0-9";
Raw2.restrict = "0-9";
Raw1.restrict = "0-9";
//-------------------------------borders----------------------------
PPP1.border = true;
PPP2.border = true;
PPP3.border = true;
PPP4.border = true;
PPP5.border = true;
PPP6.border = true;
PPP7.border = true;
Cntbonus.border = true;
RawPrice.border = true;
AverSal.border = true;
Tax.border = true;
NoC7.border = true;
NoC6.border = true;
NoC5.border = true;
NoC4.border = true;
NoC3.border = true;
NoC2.border = true;
NoC1.border = true;
Em7.border = true;
Em6.border = true;
Em5.border = true;
Em4.border = true;
Em3.border = true;
Em2.border = true;
Em1.border = true;
Raw5.border = true;
Raw4.border = true;
Raw3.border = true;
Raw2.border = true;
Raw1.border = true;
//--------------------------calculations-------------------------------
calc_btn.addEventListener(MouseEvent.CLICK, Calco);
function Calco(event:MouseEvent):void;
{
iPPP1 = PPP1.text;
iPPP2 = PPP2.text;
iPPP3 = PPP3.text;
iPPP4 = PPP4.text;
iPPP5 = PPP5.text;
iPPP6 = PPP6.text;
iPPP7 = PPP7.text;
iCntbonus = Cntbonus.text;
iRawPrice = RawPrice.text;
iAverSal = AverSal.text;
iTax = Tax.text;
iNoC7 = NoC7.text;
iNoC6 = NoC6.text;
iNoC5 = NoC5.text;
iNoC4 = NoC4.text;
iNoC3 = NoC3.text;
iNoC2 = NoC2.text;
iNoC1 = NoC1.text;
iEm7 = Em7.text;
iEm6 = Em6.text;
iEm5 = Em5.text;
iEm4 = Em4.text;
iEm3 = Em3.text;
iEm2 = Em2.text;
iEm1 = Em1.text;
iRaw5 = Raw5.text;
iRaw4 = Raw4.text;
iRaw3 = Raw3.text;
iRaw2 = Raw2.text;
iRaw1 = Raw1.text;
i1 = (parseInt(iEm1) + parseInt(iNoC1)) * 10 * bonus;
i2 = (parseInt(iEm2) + parseInt(iNoC2)) * 10 * bonus;
i3 = (parseInt(iEm3) + parseInt(iNoC3)) * 10 * bonus;
i4 = (parseInt(iEm4) + parseInt(iNoC4)) * 10 * bonus;
i5 = (parseInt(iEm5) + parseInt(iNoC5)) * 10 * bonus;
i6 = (parseInt(iEm6) + parseInt(iNoC6)) * 10 * bonus;
i7 = (parseInt(iEm7) + parseInt(iNoC7)) * 10 * bonus;
if (parseInt(iCntbonus) == 0) {
bonus = 1;
} else if (parseInt(iCntbonus) == 1) {
bonus = 1,2;
} else if (parseInt(iCntbonus) == 2) {
bonus = 1,4;
} else if (parseInt(iCntbonus) == 3) {
bonus = 1,6;
} else if (parseInt(iCntbonus) == 4) {
bonus = 1,8;
} else {
bonus = 2;
}
// υπολογισμός εσόδων
apotelesma = (Number(iRawPrice)*bonus*parseInt(iRaw1)*35)+(Number(iRawPrice)*bonus*parseInt(iRaw2)*70)+(Number(iRawPrice)*bonus*parseInt(iRaw3)*125)+(Number(iRawPrice)*bonus*parseInt(iRaw4)*175)+(Number(iRawPrice)*bonus*parseInt(iRaw5)*250)
apotelesma = apotelesma + (i1 * Number(iPPP1)) - (i1 * (parseInt(iTax) / 100) + (i2 * Number(iPPP2)) - (i2 * (parseInt(iTax) / 100) + (i3 * Number(iPPP3)) - (i3 * (parseInt(iTax) / 100) + (i4 * Number(iPPP4)) - (i4 * (parseInt(iTax) / 100) + (i5 * Number(iPPP5)) - (i5 * (parseInt(iTax) / 100) + (i6 * Number(iPPP6)) - (i6 * (parseInt(iTax) / 100) + (i7 * Number(iPPP7)) - (i7 * (parseInt(iTax) / 100)
apotelesma.toString()
}
I have problem with the last 3 lines...
You have some very long calculations, and are missing atleast 5 closing parenetheses. I pasted the 2nd to the last line (the one that starts with apotelesma = apotelesma) into a text editor and counted the numbers of opening/closing parentheses.
The calculation is so long, it's not immediately clear where they should be inserted. I would break down the calculations so that they are more readable. Anyone maintaining this code after you will benefit from the clarity.
Either break the long calculations up into many smaller ones, or just add line breaks in the code so it's clear what you're trying to do, like this:
apotelesma = apotelesma +
(i1 * Number(iPPP1)) -
(i1 * (parseInt(iTax) / 100) + // NOTE: you seem to be missing a closing paren here
(i2 * Number(iPPP2)) -
(i2 * (parseInt(iTax) / 100) + // and here... (and so on)
...

as3 multidimensional array

Im trying to make a multidimensional array but I obtain an error ("TypeError: Error #1010: A term is undefined and has no properties.").
var matriz:Array = new Array();
for(var p:Number = 0; p<2;p++ ){
for(var q:Number = 0; q<2;q++ ){
matriz[p][q] = 0;
}
}
what am I doing wrong?
Thanks in advance!
You need to create an array within matriz[p] before you can add an array (or anything else) into it.
You can achieve what you're attempting without errors like this:
var matriz:Array = [];
for(var p:Number = 0; p<2; p++)
{
// Create an array at matriz[p] if undefined.
if(matriz[p] == undefined) matriz[p] = [];
for(var q:Number = 0; q<2; q++)
{
matriz[p][q] = 0;
}
}
Essentially you were trying to do the same as this:
var object:Object = {};
object.nonexistantProperty.value = 10;
What Marty has said is correct, however I prefer removing the if condition and changing the code to the following:
var matriz:Array = [];
for(var p:Number = 0; p<2; p++) {
matriz[p] = [];
for(var q:Number = 0; q<2; q++) {
matriz[p][q] = 0;
}
}
public class cArray
{
private var DIM1CAP:uint=0;
private var DIM2CAP:uint=1;
private var DIM3CAP:uint=2;
private var DIM4CAP:uint=3;
private var DIM5CAP:uint=4;
public function cArray():void
{
// avoid the noid
}
// returns empty array of args.length dimensions
// 1st argument is dim 1 capacity; 2nd is dim 2, etc.
public function getArray ( ... args ):Array
{
var arr = new Array();
if ( paramsNotValid(args) )
{
return null;
}
switch (args.length)
{
case 2:
arr = get2DArray ( args[0], args[1] );
break;
case 3:
arr = get3DArray ( args[0], args[1], args[2] );
break;
case 4:
arr = get4DArray ( args[0], args[1], args[2], args[3] );
break;
case 5:
arr = get5DArray ( args[0], args[1], args[2], args[3], args[4] );
break;
default:
break;
}
return arr;
}
// returns empty 2d array of parameter specified capacity
private function get2DArray ( _1stDimCapacity:uint, _2ndDimCapacity:uint ):Array
{
var arr2d:Array = [];
var arr1d = new Array();
for ( var i:uint=0; i<_1stDimCapacity; i++ )
{
arr1d[_2ndDimCapacity-1] = undefined;
arr2d.push(arr1d);
arr1d = new Array();
}
return arr2d;
}
// returns empty 3d array of parameter specified capacity
private function get3DArray ( dim1Cap:uint,
dim2Cap:uint,
dim3Cap:uint ):Array
{
var arr3d = new Array();
for ( var i:uint=0; i<dim1Cap; i++ )
{
arr3d.push ( get2DArray ( dim2Cap, dim3Cap ) );
}
return arr3d;
}
// returns empty 4d array of parameter specified capacity
private function get4DArray ( dim1Cap:uint,
dim2Cap:uint,
dim3Cap:uint,
dim4Cap:uint):Array
{
var arr4d = new Array();
for ( var i:uint=0; i<dim1Cap; i++ )
{
arr4d.push ( get3DArray ( dim2Cap, dim3Cap, dim4Cap ) );
}
return arr4d;
}
// returns empty 5d array of parameter specified capacity
private function get5DArray ( dim1Cap:uint,
dim2Cap:uint,
dim3Cap:uint,
dim4Cap:uint,
dim5Cap:uint):Array
{
var arr5d = new Array();
for ( var i:uint=0; i<dim1Cap; i++ )
{
arr5d.push ( get4DArray ( dim2Cap, dim3Cap, dim4Cap, dim5Cap ) );
}
return arr5d;
}
//////////////////////////////////////////////////////
private function paramsNotValid ( args:Array ):Boolean
{
if ( args.length<2 || args.length>5 )
{
return true;
}
for ( var i:uint=0; i<args.length; i++ )
{
if ( ! ( args[i]>0 ) )
{
break;
}
}
if ( i < args.length )
{
return true;
}
return false;
}
}
}
public class cMain extends MovieClip
{
var cArr:cArray = new cArray;
public function cMain():void
{
var arr2d:Array;
var arr3d:Array;
var chessBrd_4d:Array;
var arr5d:Array;
// capacity of 10 games; 150 moves/gm; white's mv or black's;
// - piece positions; move in chess notation (index 32 of
// - last dimension); commentary (index 33 of last dimension)
chessBrd_4d = cArr.getArray(10,150,2,34);
// adding data
// - 4th game, 8th move, white's move, positions of pieces
chessBrd_4d[3][7][0][0] = 'd8';
chessBrd_4d[3][7][0][1] = 'b1';
// ...
// - positions of pieces up to last one
// ...
// - last piece pos
chessBrd_4d[3][7][0][31] = 'captured';
// - actual move in chess notation
chessBrd_4d[3][7][0][32] = 'nC4';
// - annotation
chessBrd_4d[3][7][0][33] = 'blocks b pawn, ' +
'opens diag for c1 bishop, ' +
'Justin Beiber is a putz, ' +
'the president is liar'
trace ( 'piece 0 is on square ' + chessBrd_4d[3][7][0][0]);
trace ( 'piece 1 is on square ' + chessBrd_4d[3][7][0][1]);
trace ( ' ... ' )
trace ( 'piece 31 has been ' + chessBrd_4d[3][7][0][31]);
trace ( 'move: ' + chessBrd_4d[3][7][0][32]);
trace ( chessBrd_4d[3][7][0][33]);
/*
trace results:
piece 0 is on square d8
piece 1 is on square b1
...
piece 31 has been captured
move: nC4
blocks b pawn, opens diag for c1 bishop,
Justin Beiber is a putz, the president is liar
*/
/*
trace ( chessBrd_4d.length ); = 10
trace ( chessBrd_4d [ 2 ].length ); = 150
trace ( chessBrd_4d [ 2 ] [ 4 ].length ); = 2
trace ( chessBrd_4d [ 2 ] [ 4 ] [ 1 ].length ); = 34
trace ( chessBrd_4d [ 2 ] [ 4 ] [ 0 ] .length); = 34
*/
}
}