Copy link to clipboard
Copied
So i am currently trying to make 5 boxes/ships (3Blue and 2 Green)appear in a random position(1st-Gree,2nd- Blue,3rd-blue,4th-Green,5th-Green or in some other random way) i the upper corner of the stage.Make them move down.Then make a new 5 boxes and position them in the same starting position as the previous 5 and make only them get randomised (not with the previous 5,what it is doing at the moment).And do this while they are moving down. so the code so far i have is this :
import flash.events.Event;
var shipCount:Number;
var shipCount2:Number;
var shipArray:Array = new Array();
var shipArrayPosition:Array = new Array();
var counter:int = 0
var positionsX:Array = ["50","100","150","200","250",]
addEventListener(Event.ENTER_FRAME, everyFrame)
function everyFrame(ev:Event):void{
counter++
if(counter % 70 == 0){
doShips()
}
positionShips()
trace(shipArray.length )
}
function doShips() {
shipCount = 3;
shipCount2 = 2;
var gap = 10;
for (var i:int = 0; i < shipCount; i++) {
var s = new Ship;
shipArray.push(s);
//s.x = s.width/2 + (s.width* i) + gap*i
//addChild(s)
//shipArrayPosition.push(s);
}
for (var j:int = 0; j < shipCount2; j++) {
s = new Ship2;
shipArray.push(s);
}
var array:Array=new Array();
while (shipArray.length>0) {
var index:uint = Math.floor(Math.random() * shipArray.length);
array.push(shipArray[index]);
shipArray.splice(index,1);
}
shipArray = array;
//shipsArray has been randomized
for (var k:int = shipArray.length - 1; k >= 0; k--) {
shipArray
.x = positionsX addChild(shipArray
) }
}
function positionShips() {
for (var i:int = shipArray.length - 1; i >= 0; i--) {
shipArray.moveDown() //what the code in the Ship and Ship2 class does -> only: this.y += 3
}
}
and how to make them stay at one position.Not to move in any X position when the new 5 are added
This one is more efficient yet because it removes ships that moved out of view:
...import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
var numShips:int = 5;
var shipCount:int = 3;
var shipArray:Vector.<Sprite> = new Vector.<Sprite>();
var counter:int = 0
var positionsX:Array = [];
init();
function init():void
{
// populate positions
for (var i:int = numShips - 1; i >= 0; i--)
positionsX.push(50 * i);
addEventListener(Event.ENT
Copy link to clipboard
Copied
Does the following work for you in principal?
import flash.display.MovieClip;
import flash.events.Event;
var shipCount:int = 3;
var shipArray:Array = new Array();
var counter:int = 0
var positionsX:Array = [50, 100, 150, 200, 250];
addEventListener(Event.ENTER_FRAME, everyFrame)
function everyFrame(e:Event):void
{
counter++
if (counter % 70 == 0)
doShips();
positionShips();
trace(shipArray.length);
}
function doShips():void
{
var gap = 10;
shuffle(positionsX);
var s:MovieClip;
for (var i:int = 0; i < positionsX.length; i++)
{
s = i < shipCount ? new Ship() : new Ship2();
s.x = int(positionsX);
shipArray.push(addChild(s));
}
}
function shuffle(a:Array):void
{
for (var i:int = a.length - 1; i >= 0; i--)
{
var r:int = Math.floor(Math.random() * (i + 1));
var t:Object = a
; a
= a; a = t;
}
}
function positionShips():void
{
for each (var ship:MovieClip in shipArray)
ship.y += 3;
}
Copy link to clipboard
Copied
This one is faster:
import flash.display.MovieClip;
import flash.events.Event;
var numShips:int = 5;
var shipCount:int = 3;
var shipArray:Vector.<MovieClip> = new Vector.<MovieClip>();
var counter:int = 0
var positionsX:Array = [];
init();
function init():void
{
for (var i:int = numShips - 1; i >= 0; i--)
{
positionsX.push(50 * i);
}
addEventListener(Event.ENTER_FRAME, everyFrame);
}
function everyFrame(e:Event):void
{
counter++
if (counter % 70 == 0)
doShips();
positionShips();
trace(shipArray.length);
}
function doShips():void
{
var gap = 10;
shuffle(positionsX);
var s:MovieClip;
for (var i:int = numShips - 1; i >= 0; i--)
{
s = i < shipCount ? new Ship() : new Ship2();
s.x = positionsX;
shipArray.push(addChild(s));
}
}
function shuffle(a:Array):void
{
for (var i:int = a.length - 1; i >= 0; i--)
{
var r:int = Math.floor(Math.random() * (i + 1));
var t:Object = a
; a
= a; a = t;
}
}
function positionShips():void
{
for each (var ship:MovieClip in shipArray)
ship.y += 3;
}
Copy link to clipboard
Copied
And this one is even better - less things to move:
import adobe.utils.CustomActions;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
var numShips:int = 5;
var shipCount:int = 3;
var shipArray:Vector.<Sprite> = new Vector.<Sprite>();
var counter:int = 0
var positionsX:Array = [];
init();
function init():void
{
for (var i:int = numShips - 1; i >= 0; i--)
positionsX.push(50 * i);
addEventListener(Event.ENTER_FRAME, everyFrame);
}
function everyFrame(e:Event):void
{
counter++
if (counter % 70 == 0)
doShips();
positionShips();
trace(shipArray.length);
}
function doShips():void
{
var shipsLine:Sprite = new Sprite();
shipsLine.cacheAsBitmap = true;
var gap = 10;
shuffle(positionsX);
var s:MovieClip;
for (var i:int = numShips - 1; i >= 0; i--)
{
s = i < shipCount ? new Ship() : new Ship2();
s.x = positionsX;
shipArray.push(addChild(shipsLine.addChild(s)));
}
}
function shuffle(a:Array):void
{
for (var i:int = a.length - 1; i >= 0; i--)
{
var r:int = Math.floor(Math.random() * (i + 1));
var t:Object = a
; a
= a; a = t;
}
}
function positionShips():void
{
for each (var shipLine:Sprite in shipArray)
shipLine.y += 3;
}
Copy link to clipboard
Copied
This one is more efficient yet because it removes ships that moved out of view:
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
var numShips:int = 5;
var shipCount:int = 3;
var shipArray:Vector.<Sprite> = new Vector.<Sprite>();
var counter:int = 0
var positionsX:Array = [];
init();
function init():void
{
// populate positions
for (var i:int = numShips - 1; i >= 0; i--)
positionsX.push(50 * i);
addEventListener(Event.ENTER_FRAME, everyFrame);
}
function everyFrame(e:Event):void
{
counter++
if (counter % 70 == 0)
doShips();
positionShips();
trace(shipArray.length);
}
function doShips():void
{
var gap = 10;
var s:MovieClip;
var shipsLine:Sprite = new Sprite();
shipsLine.cacheAsBitmap = true;
addChild(shipsLine);
shipArray.push(shipsLine);
shuffle(positionsX);
for (var i:int = numShips - 1; i >= 0; i--)
{
s = i < shipCount ? new Ship() : new Ship2();
s.x = positionsX + gap;
shipsLine.addChild(s);
}
shipsLine.y = -shipsLine.height;
}
function shuffle(a:Array):void
{
for (var i:int = a.length - 1; i >= 0; i--)
{
var r:int = Math.floor(Math.random() * (i + 1));
var t:Object = a
; a
= a; a = t;
}
}
function positionShips():void
{
for each (var shipLine:Sprite in shipArray)
{
shipLine.y += 3;
// remove line that moved out of stage
if (shipLine.y > stage.stageHeight + shipLine.height)
{
removeChild(shipLine);
shipArray.splice(shipArray.indexOf(shipLine), 1);
}
}
}
Copy link to clipboard
Copied
Woallllll 4 solutions from only 1 man.... I`ve never dreamed for that help ;))) thanks man 1!! and the code .... it does exactly what i wanted ! yet can you tell me what this does with simple words cause ive never used it s = i < shipCount ? new Ship() : new Ship2();
Copy link to clipboard
Copied
Line
s = i < shipCount ? new Ship() : new Ship2();
is a literal notation for if...else
it has the same outcome as:
if(i < shipCount)
s = new Ship();
else
s = new Ship2();
s = i < shipCount ? [question mark validates if assertion before it - in this case "i < shipCount" - is true] new Ship() [if true - this piece is executed] : [colon separates code that is executed if first assertion is false] new Ship2();
Literal notations are faster and less verbose.
Other examples of literals are
var a:Array = [] instead of new Array();
var o:Object = {} instead of new Object();
Find more inspiration, events, and resources on the new Adobe Community
Explore Now