- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsHi ya
I am writing a retro computer game for my kids. Its based on a top down scrolling shoot em up like Xenon 2 back in the early-mid 80's (showing my age there).
I have all the basics of the game working such a player movement, different aliens, firing, score etc. etc.
At the moment thought, my alien sprites are moving in a random manner i.e. they move at a set angle and velocity until they hit the edge of the screen and then pick a new angle and speed.
I'm hoping someone has some ideas on how I can get my sprites to move in patterns or loops, like in the old shoot-em-ups like Xenon 2.
I'm not sure I'm making much sense, but I hope this triggers something and someone has some ideas on how I could implement such movement for the sprites. I have pasted below the class which controls an enemy sprite (actor) within the game. The act() method is called during each cycle of the main game loop to update what the sprite is doing.
Any and all help appreciated.
Thanks
/*
* Alien.java
*
* Created on Sep 17, 2007, 8:08:22 PM
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.daleyuk.alieninvaders.
import com.daleyuk.alieninvaders.
/**
* Extends the base Actor class and defines a monster in the game
* @author mikedaley
*/
public class Alien extends Actor {
/**
* At present the monsters only move from left to right, so we have an x axis velocity variable
*/
protected int velocity;
protected boolean isDead;
protected double angle;
protected static final double FIRING_FREQUENCY = 0.04;
/**
* We define the stage on which the monster will belong and set up the two frames of animation plus the framespeed
* More details on the base Actor class are found in Actor.java
* @param stage Stage
*/
public Alien(Stage stage) {
super(stage);
this.stage = stage;
setSpriteName(new String[] {"newalien2.gif"});
setFrameSpeed(2);
isDead = false;
angle = Math.random() * 360;
}
/**
* This overrides the base Actor method for act. It telles the monster to move from left to right with a velocity of
* vX. This velocity is defined when the monster is defined and each instance of monster can have its own speed.
* I need to work on this to provide a much more interesting movement for the monsters.
*/
@Override
public void act() {
super.act();
//Calculate the x and Y based on the current velocity and angle of the alien
x += velocity * Math.cos(angle);
y += velocity * Math.sin(angle);
//If the monster is about to leave the viewable screen, pick a random anlge
//so that it changes direction
if (x <= 0 || x >= stage.WIDTH || y <= 0 || y >= stage.PLAY_HEIGHT - getHeight()) {
angle = (Math.random() * 359) + 1;
velocity = (int) (Math.random()*10-4) + 1;
if (velocity <= 0) {
velocity = 1;
}
}
if (x >= stage.getPlayer().getX() - 10 && x <= stage.getPlayer().getX() + 10 && stage.getPlayer().getY() >= y && Math.random() < FIRING_FREQUENCY) {
fire();
}
}
/**
* This method is called on a random frequency. When is is called it creates a new instance of the laser
* actor and places it on the stage from the location of the alien firing and centred to the middle of the
* alien sprite. It also plays the sound of the laser firing.
* */
public void fire() {
Laser m = new Laser(stage);
m.setX(x + getWidth() / 2);
m.setY(y + getHeight());
stage.addActor(m);
stage.getSoundCache().play
}
/**
* If the alien collides with a bullet, shield, multilaster or player then mark for removal, place an explosion sprite on screen
* and add to the players score
*/
@Override
public void collision(Actor a) {
if (a instanceof Bullet || a instanceof MultiLaser || a instanceof Shield) {
remove(); //Remove the aliean from the stage
Explosion e = new Explosion(stage, this.getX() - a.getWidth() / 2, this.getY() - a.getHeight() / 2);
stage.addActor(e); //Add the explosion to the stage
stage.getSoundCache().play
spawn(); //Spawn a new alien
}
if (a == stage.getPlayer()) {
remove(); //Remove the aliean from the stage
Explosion e = new Explosion(stage, this.getX() - a.getWidth() / 2, this.getY() - a.getHeight() / 2);
stage.addActor(e); //Add the explosion to the stage
stage.getSoundCache().play
spawn(); //Spawn a new alien
}
}
/**
* Basic method to place an alien on the stage. Used when an alien is killed for example
* */
public void spawn() {
Alien m = new Alien(stage);
m.setX((int) (Math.random() * Stage.WIDTH - m.getWidth()));
m.setY((int) (Math.random() * Stage.PLAY_HEIGHT / 2));
m.setVelocity((int) (Math.random()*10-4) + 1);
if (velocity <= 0) {
velocity = 1;
}
stage.addActor(m);
}
/**
* Getters and setters
* @return int Velocity
*/
public int getVelocity() {
return velocity;
}
public void setVelocity(int velocity) {
if (velocity != 0) {
this.velocity = velocity;
} else {
this.velocity = 0;
}
}
public double getAngle() {
return this.angle;
}
}
This question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership