Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › ActionScript 3 help- Adding enemies space invaders style?
New Posts  All Forums:Forum Nav:

ActionScript 3 help- Adding enemies space invaders style?

post #1 of 6
Thread Starter 
Hey everyone,

Need some serious help here. Trying to create some enemies but every resource I find doesn't seem to work or will just confuse me... So I was hoping for some more personal help.

Basically I wanna add some enemies in preferably more than one in rows space invaders style. I imagine it'd be some sort of if statements chain (if alien touches right wall move down and reverse direction etc etc) but I'm not sure how to go about setting this up... Also I'd like to create some other paths as well as the space invader rows...

Help?
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #2 of 6
I know next to nothing about actionscript but as for a standard space invader system basically all the invaders in your grid will move the same direction and drop at approximately the same time. so really if they are moving right you can just check the right most column for it to get to your drop down spot, drop the grid of enemies then start them moving left and checking left most column for the drop down area. As for other paths that depends on what you want to do with it. What I would personally do would be to take 1 enemy make him move the way I want and then expand from there but that is just me and may not be the best way.
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
Reply
post #3 of 6
Hi,
Im currently doing an Advanced Flash course and this is the code we used to get a very basic game going, not sure how relevant it is for your game.
But hope this helps or gives you some ideas to push forward with the game.
Code:
import flash.sensors.Accelerometer;
var gameTimer:Timer = new Timer(1000);
function onTick(event:TimerEvent):void
{
tbTimer.text = String(Math.floor(event.target.currentCount));
}
gameTimer.addEventListener(TimerEvent.TIMER, onTick);

function onTimerComplete(event:TimerEvent):void
{
tbMessage.text = "Time's up - Game Over";
}
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);

function startTimer(event:MouseEvent):void
{
gameTimer.start();
}
btStart.addEventListener(MouseEvent.CLICK,startTimer);
function stopTimer(event:MouseEvent):void
{
gameTimer.stop();
}
btStop.addEventListener(MouseEvent.CLICK,stopTimer);
function getRandom(min, range):Number
{
var randomNumber:Number = Math.floor((Math.random()* range) + min);
return randomNumber;
}
//Create a variable called i and initialize it to 1
var i:int = 1;
//Math.random()* rangeValue) + lower limit;
//Create a variable called numHits and set to 0
var numHits:int = 0;
//Math.floor (Math.random() * 40) + 50;
//Create an empty array called spaceships
var spaceships:Array = new Array();
function doEveryFrame(event:Event):void
{
var newRandom = getRandom(0,20);
if (newRandom < 2)
{
var spaceship:shipMC = new shipMC();
this.addChild(spaceship);
spaceship.x = -10;
var scaleValue = (getRandom(30, 99))/100;
spaceship.scaleX = scaleValue;
spaceship.scaleY = scaleValue;
var ycoord = getRandom(50,250);
spaceship.y = ycoord;
function clickHandler()
{
numHits++;
tbHits.text = String(numHits);
removeChild(spaceship);
}
spaceship.addEventListener(MouseEvent.CLICK, clickHandler);
spaceships[i] = spaceship;
}
i +=  1;
}
this.addEventListener(MouseEvent.MOUSE_MOVE,doEveryFrame);
//New Code for event_frame and custom cursor
//create listener and associate the moveREct handle
//Create Custom Cursor to stage;
var cursor: cursorMC = new cursorMC();//create displayObject for cursor using mc symbol 
stage.addChild(cursor);
//add it to display list. Using stage ensure it is always on top ;
cursor.mouseEnabled = false;//stop it blocking mouse events 
Mouse.hide();
//self explanatory ;
function moveCursor(event:MouseEvent):void
{
cursor.x = event.stageX;//set cursor position to mouse postion 
cursor.y = event.stageY;// as above 
event.updateAfterEvent();
}
//stops cursor movement being "jerky" ;
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveCursor);//you know this by now
Blue Devil's Mist
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD PhenomII x6 1090T ASRock Fatal1ty 990FX Pro Upgrade soon to HD7870 GSkill RipjawX 4x4GB DDR3 1600Mhz 
Hard DriveOptical DriveOSMonitor
Kingston V 30GB SSD + WD 2x150GB VRaptor 1x2TB Pioneer 218BK Windows7 Ult [x64] 2x 23' Dell U2312HM 
PowerCase
Corsair AX-850W Antec 1200 
  hide details  
Reply
Blue Devil's Mist
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD PhenomII x6 1090T ASRock Fatal1ty 990FX Pro Upgrade soon to HD7870 GSkill RipjawX 4x4GB DDR3 1600Mhz 
Hard DriveOptical DriveOSMonitor
Kingston V 30GB SSD + WD 2x150GB VRaptor 1x2TB Pioneer 218BK Windows7 Ult [x64] 2x 23' Dell U2312HM 
PowerCase
Corsair AX-850W Antec 1200 
  hide details  
Reply
post #4 of 6
This might help. Wonderfl is a WONDERFL site for as3.
Synthkart
(13 items)
 
  
CPUMotherboardGraphicsRAM
1055T Asus Onboard x4200 2x2GB GeIL DDR 1333 (9-9-9) 
Hard DriveOptical DriveOSMonitor
Seagate 320GB Sata3.0 Liteon iHAS124 Win7 x64 Some old crt 
KeyboardPowerCaseMouse
digital Raidmax-450K Some cheap raidmax Labtec 
Mouse Pad
  hide details  
Reply
Synthkart
(13 items)
 
  
CPUMotherboardGraphicsRAM
1055T Asus Onboard x4200 2x2GB GeIL DDR 1333 (9-9-9) 
Hard DriveOptical DriveOSMonitor
Seagate 320GB Sata3.0 Liteon iHAS124 Win7 x64 Some old crt 
KeyboardPowerCaseMouse
digital Raidmax-450K Some cheap raidmax Labtec 
Mouse Pad
  hide details  
Reply
post #5 of 6
Seem's like the issue here is you're not entirely clear on what you are trying to solve (which is not a big deal, we all are at some point).

Can you should probably break the problem down into chunks and post back what you have. It will help you and I to get a better understanding of exactly what you need. I could probably help you better that way.
Nov 7 2010
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 930 Asus P6X58D-E XFX Radeon HD 6870 Kingston HyperX DDR3-1600 6 Gig 
Hard DriveOSKeyboardPower
Crucial C300 64 Gig Windows 7 Professional Logitech G15 Corsair TX750W 
CaseMouse
Coolermaster CM690 Logitech G5 
  hide details  
Reply
Nov 7 2010
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 930 Asus P6X58D-E XFX Radeon HD 6870 Kingston HyperX DDR3-1600 6 Gig 
Hard DriveOSKeyboardPower
Crucial C300 64 Gig Windows 7 Professional Logitech G15 Corsair TX750W 
CaseMouse
Coolermaster CM690 Logitech G5 
  hide details  
Reply
post #6 of 6
Quote:
Basically I wanna add some enemies in preferably more than one in rows space invaders style. I imagine it'd be some sort of if statements chain (if alien touches right wall move down and reverse direction etc etc) but I'm not sure how to go about setting this up... Also I'd like to create some other paths as well as the space invader rows...
Quote:
Seem's like the issue here is you're not entirely clear on what you are trying to solve
I think c_loke nailed this one.

@ the OP:

I think you've got a couple of challenges ahead and you haven't separated them out from one another yet. From what you describe, you first need to figure out how to place lines of enemies onscreen. Then, you need to figure out how to make them move so that they will move sideways until they touch the wall and reverse directions.

So you've got some code that adds enemies to your level, and you've got some code that defines how enemies move. Two separate things. No need to think about them at the same time.

Now, I have no idea how you've built your code so far, but I'm going to assume that you've got some class for your enemies that you can instantiate as many times as you want to make as many as you need. All you'll really need to do, if you have done that much already, is make a loop in your level setup code that varies the x and y position of enemies as it creates them.

Making an enemy move is pretty trivial, and I imagine you've got a handle on that or you probably wouldn't even be trying to make a game in the first place. Making it switch directions when it hits the edge of the screen is pretty easy too. As you said, it's pretty much just an if statement that will check if the x position of the enemy is less than some boundary on the left or greater than some boundary on the right, and switch the direction of movement.

But making the whole group switch directions when only one of them hits the wall -- Space Invaders style -- is a little more complex than that, because the enemy that hits the wall has to communicate to all the other enemies that it is switching direction, and that all the other enemies need to switch at the same time.

Luckily, AS3 is equipped with Events that you can use to make this happen without a whole lot of trouble. In your code for detecting a side boundary, when you switch direction dispatch a custom event too:

dispatchEvent(new Event("Switch direction now guys!"));

Your enemy class has to extend EventDispatcher to do this, but MovieClip extends EventDispatcher so you're probably okay. In case you're wondering, the String can be anything -- as long as it's unique from your other Events.

But the tricky part of this is listening for these Events... It's totally impractical to have EventListeners on each enemy listening for Events from every other enemy. It would be a disaster to write or event to add another enemy to the screen. But there's an easy way to do this. Try this code to dispatch your event instead:

dispatchEvent(new Event("Switch direction now guys!", true));

This turns on Event bubbling, which will cause dispatched Events of this type to pass up through the enemy's parents. Which conveniently means the stage, which all your enemies are probably children of. So you can just put an EventListener on the stage to detect when an enemy calls for the others to switch directions, then in the event handler call a direction switch function in your enemy class for each enemy instance that should change direction. All you need to make this possible is to keep a list of your enemies in an array somewhere so you can find them all when you need to switch them up.

I know that was kind of a wall of text and I didn't really supply any code, but hopefully it's helpful to you. I think writing things this way will solve a lot of your problems and keep things fairly simple. Just ask if you'd like me to clarify anything.
Edited by sivyr - 6/22/11 at 6:29am
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › ActionScript 3 help- Adding enemies space invaders style?