Hit Test bounce - actionscript-3

I'm trying to make sort of a Balloon fight game for flash. I have a ceiling at the top of the screen, so when the player hits it, it should bounce off of it. Problem is, I don't know how to go about doing that. Here is the code.
I was messing around with numbers. In the upBumping variable, where the problem lies, I wanted to have the character bounce down once it hits, but it only does this once. After, it just goes through the ceiling. Perhaps I'm going about this the wrong way?
//gravity and stuff
var gravity:Number = 0.3;
var fall:Number = 0.3;
var downBumping:Boolean = false;
var upBumping:Boolean = false;
//direction I guess
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var spaceKeyDown:Boolean = false;
//character movement ya'll
var mainSpeed:Number = 6;
stage.addEventListener(Event.ENTER_FRAME, game);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
function game(e:Event): void{
//if certain keys are down then move dat fool
if(leftKeyDown){
Monkey.x -= mainSpeed;
}
if(rightKeyDown){
Monkey.x += mainSpeed;
}
if(upKeyDown){
fall = fall-0.9;
gravity = 0.3;
}
if(Floor.hitTestObject(Monkey)){
downBumping = true;
trace("downBumping");
}
if(Ceiling.hitTestObject(Monkey)){
upBumping = true;
trace("upBumping");
trace(Monkey.y);
}
if(upBumping){
Monkey.y += 20;
}
Monkey.y = Monkey.y+fall;
gravity = gravity*1.0;
fall = fall+gravity;
}

Here's how I would do jumping and bouncing - this does not include other code like moving left to right since you already got that
function game(e:Event):void {
if(jumping){
yVelocity += 0.1; //gravity
Monkey.y += yVelocity;
if(Floor.hitTestObject(Monkey)){
jumping = false;
while(Floor.hitTestObject(Monkey)){
y--;
}
}
if(Ceiling.hitTestObject(Monkey)){
yVelocity = 0;
while(Ceiling.hitTestObject(Monkey)){
y++;
}
}
}
if(upKeyDown && !jumping){
jumping = true;
yVelocity = -3;
}
}
Obviously, declare the variables used in this example.

Related

Having an issue with Flash Actionscript3

Wondering if you guys can help me. I've used online tutorials to create the following.
I am working on a small flash project and I've hit a wall. Basically, I've got 3 scenes. A home page, and 2 games. My home page and one of the games are programmed on the timeline within an actions layer. The third game is applied to a Main.as. The problem is that I want to apply a button called home to the Flappy game, but since I've used the .as file for this code, I'm unsure how to do it.
Basically, my question is "How do I add a button to the "Flappy" Scene? Each time i try to do it and run a test, It's playing the Flappy Game over the Scene 1.
Main.as ("Flappy" Scene3)
package{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; //used for ENTER_FRAME event
//import flash.events.MouseEvent;
public class Main extends MovieClip{
//These are all of the constants used.
const gravity:Number = 1.5; //gravity of the game. How fast ball falls
const dist_btw_obstacles:Number = 300; //distance between two obstacles
const ob_speed:Number = 8; //speed of the obstacle
const jump_force:Number = 15; //force with which it jumps
//variables
var player:Player = new Player();
var lastob:Obstacle = new Obstacle(); //varible to store the last obstacle in the obstacle array
var obstacles:Array = new Array(); //an array to store all the obstacles
var yspeed:Number = 0; //A variable representing the vertical speed of the bird
var score:Number = 0; //A variable representing the score
public function Main(){
init();
}
function init():void {
//initialize all the variables
player = new Player();
lastob = new Obstacle();
obstacles = new Array();
yspeed = 0;
score = 0;
//add player to center of the stage the stage
player.x = stage.stageWidth/2;
player.y = stage.stageHeight/2;
addChild(player);
//create 3 obstacles ()
createObstacle();
createObstacle();
createObstacle();
//Add EnterFrame EventListeners (which is called every frame) and KeyBoard EventListeners
addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
}
private function key_up(event:KeyboardEvent){
if(event.keyCode == Keyboard.SPACE){
//If space is pressed then make the bird
yspeed = -jump_force;
}
}
function restart(){
if(contains(player))
removeChild(player);
for(var i:int = 0; i < obstacles.length; ++i){
if(contains(obstacles[i]) && obstacles[i] != null)
removeChild(obstacles[i]);
obstacles[i] = null;
}
obstacles.slice(0);
init();
}
function onEnterFrameHandler(event:Event){
//update player
yspeed += gravity;
player.y += yspeed;
//restart if the player touches the ground
if(player.y + player.height/2 > stage.stageHeight){
restart();
}
//Don't allow the sheep to go above the screen
if(player.y - player.height/2 < 0){
player.y = player.height/2;
}
//update obstacles
for(var i:int = 0;i<obstacles.length;++i){
updateObstacle(i);
}
//display the score
scoretxt.text = String(score);
}
//This functions update the obstacle
function updateObstacle(i:int){
var ob:Obstacle = obstacles[i];
if(ob == null)
return;
ob.x -= ob_speed;
if(ob.x < -ob.width){
//if an obstacle reaches left of the stage then change its position to the back of the last obstacle
changeObstacle(ob);
}
//If the bird hits an obstacle then restart the game
if(ob.hitTestPoint(player.x + player.width/2,player.y + player.height/2,true)
|| ob.hitTestPoint(player.x + player.width/2,player.y - player.height/2,true)
|| ob.hitTestPoint(player.x - player.width/2,player.y + player.height/2,true)
|| ob.hitTestPoint(player.x - player.width/2,player.y - player.height/2,true)){
restart();
}
//If the bird got through the obstacle without hitting it then increase the score
if((player.x - player.width/2 > ob.x + ob.width/2) && !ob.covered){
++score;
ob.covered = true;
}
}
//This function changes the position of the obstacle such that it will be the last obstacle and it also randomizes its y position
function changeObstacle(ob:Obstacle){
ob.x = lastob.x + dist_btw_obstacles;
ob.y = 100+Math.random()*(stage.stageHeight-200);
lastob = ob;
ob.covered = false;
}
//this function creates an obstacle
function createObstacle(){
var ob:Obstacle = new Obstacle();
if(lastob.x == 0)
ob.x = 800;
else
ob.x = lastob.x + dist_btw_obstacles;
ob.y = 100+Math.random()*(stage.stageHeight-200);
addChild(ob);
obstacles.push(ob);
lastob = ob;
}
}
}
Here is the code for my main page called "Scenery" (Scene 1) which is in an actions layer of timeline.
stop();
import flash.events.MouseEvent;
sun1.addEventListener(MouseEvent.CLICK,cloudy);
function cloudy (e:MouseEvent){
cloud1.x = cloud1.x + 10;
cloud2.x = cloud2.x - 10;
}
pong_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
gotoAndStop(1, "SheepyPong");
}
dodge_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);
function mouseDownHandler3(event:MouseEvent):void {
gotoAndStop(1, "Flappy");
}
This is my code for a very simple "SheepyPong" (Scene2) game which is linked to from front page:
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var velocityX:Number = 5;
var velocityY:Number = 5;
movieClip_1.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
movieClip_1.y -= 5;
}
if (downPressed)
{
movieClip_1.y += 5;
}
sheep.x += velocityX;
sheep.y += velocityY;
if(sheep.y + sheep.height/2 > stage.stageHeight || sheep.y - sheep.height/2 < 0){
velocityY *= -1
}
if(sheep.hitTestObject(AI) || sheep.hitTestObject(movieClip_1)){
velocityX *= -1;
}
if (sheep.x < 0) {
score2.text = (int(score2.text) +1).toString();
sheep.x = 275;
sheep.y = 200;
velocityX *= -1;
}
if (sheep.x > stage.stageWidth) {
score1.text = (int(score1.text) +1).toString();
sheep.x = 275;
sheep.y = 200;
velocityX *= -1;
}
AI.y = sheep.y;
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
}//end of switch
}//end of function
home_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);
function mouseDownHandler1(event:MouseEvent):void {
gotoAndStop(1, "Scenery");
}
I'm also getting this error in output when I run it. It's not actually affecting the outpu
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/onEnterFrameHandler()[/Users/mynamehere/Documents/Game/Main.as:91]
//This is line 91 in the Main.as file:
scoretxt.text = String(score);

Gravity / velocity & jump issue (AS3, platformer)

I'm currently trying to program a flash game for Android.
I have more or less working gravity & velocity and hit tests - so I don't fall through my platforms.
The problem now is, as soon as I hit "jump", the hit test stops working and i fall through the platforms. If I set my character to a different, higher position I don't even fall down.
Can anyone help me figure this out?
Here is my code:
import flash.events.MouseEvent;
import flash.events.Event;
var gravity:Number = 2;
var velocity:Number = 1.1;
var jumpPower:Number = 0;
var isJumping:Boolean = false;
stage.addEventListener(Event.ENTER_FRAME, touchPlatform);
player.addEventListener(Event.ENTER_FRAME, appeal);
function touchPlatform(e:Event):void
{
if(kurz.hitTestObject(player))
{
hitPlatform = true;
}
else if(kurz2.hitTestObject(player))
{
hitPlatform = true;
}
}
function appeal(e:Event):void
{
gravity *= velocity;
player.y += gravity;
if(hitPlatform == true)
{
velocity = 0;
}
}
jump.addEventListener(MouseEvent.CLICK, doJump);
stage.addEventListener(Event.ENTER_FRAME, update);
function doJump(e:MouseEvent):void
{
if(!isJumping)
{
jumpPower = 30;
isJumping = true;
}
}
function update(e:Event):void
{
if(isJumping)
{
player.y -= jumpPower;
jumpPower -= 2;
}
else
{
isJumping = false;
}
}
Your issue is that once you start jumping, you never stop! No where (that can be reached) do you set isJumping to false. Also, your jumping value and your gravity are currently running in tandem, you only want one OR the other affecting your player at any given time.
Try something like this (see code comments for explainations)
import flash.events.MouseEvent;
import flash.events.Event;
var gravity:Number = 2;
var velocity:Number = 1.1;
var jumpPower:Number = 0;
var isJumping:Boolean = false;
jump.addEventListener(MouseEvent.CLICK, doJump);
function doJump(e:MouseEvent):void {
if(!isJumping){
jumpPower = 30;
isJumping = true;
}
}
//Just one ENTER_FRAME handler is better,
//then you have more control over the order in which code gets run
//I've combined your three into one and called it gameLoop
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function gameLoop(e:Event):void {
//this is the same, I just combined your 2 if's into one.
if(kurz.hitTestObject(player) || kurz2.hitTestObject(player)){
hitPlatform = true;
}
//you should set the velocity before assigning it to the player
if(hitPlatform == true){
velocity = 0;
gravity = 2; //you probably want to reset gravity to default too
isJumping = false; //SET isJumping to false now that you've hit a platform! <-------------
}else{
velocity = 1.1; //you need to reset velocity when not on a platform
}
//Now that we've determined the velocity and if we're jumping, let's move the player the appropriate amount
if(isJumping){
//Since we're currently jumping, use the jumpPower instead of gravity
player.y -= jumpPower;
jumpPower -= 2;
}else{
if(!hitPlatform){
//Since we're NOT JUMPING, and not on a platform, use gravity.
gravity *= velocity;
player.y += gravity;
}
}
}

Flash Collision Detection (Platformer game)

I am trying to make a platformer game in flash, using ActionScript 3. Currently everything is working except for one thing.
The Problem:
When the player collides with the bottom of a platform, if his xVel is not equal to zero, the horizontal collision detection loop is called and the player is moved horizontally as well as vertically. This means he bounces off the platform from underneath but is also displaced to one side of the platform. If the player's xVel is equal to zero, everything works fine. This is because the horizontal collision loop is not called. I cannot figure out why this happens. Any help would be greatly appreciated.
The Code:
import flash.events.Event;
import flash.geom.Rectangle;
var level:Array = new Array();
var xVel = 0;
var yVel = 0;
var xSpeed = 15;
var accel =1.5;
var grav = 2;
var jumpHeight = 15*grav;
for(var i = 0; i<numChildren;i++){
if(getChildAt(i) is platform){
level.push(getChildAt(i).getRect(this));
}
}
var upKeyDown = false;
var leftKeyDown = false;
var rightKeyDown = false;
var downKeyDown = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
stage.addEventListener(Event.ENTER_FRAME,gameLoop);
function keyDown(e:KeyboardEvent){
if(e.keyCode == Keyboard.UP){
upKeyDown = true;
}
if(e.keyCode == Keyboard.LEFT){
leftKeyDown = true;
}
if(e.keyCode == Keyboard.RIGHT){
rightKeyDown = true;
}
if(e.keyCode == Keyboard.DOWN){
downKeyDown = true;
}
}
function keyUp(e:KeyboardEvent){
if(e.keyCode == Keyboard.UP){
upKeyDown = false;
}
if(e.keyCode == Keyboard.LEFT){
leftKeyDown = false;
}
if(e.keyCode == Keyboard.RIGHT){
rightKeyDown = false;
}
if(e.keyCode == Keyboard.DOWN){
downKeyDown = false;
}
}
function gameLoop(e:Event){
if(rightKeyDown){
if(xVel<xSpeed){
xVel+=accel;
}
}else if(leftKeyDown){
if(xVel>-xSpeed){
xVel-=accel;
}
}else{
xVel *=0.6;
}
//horizontal
player.x+=xVel;
for(i = 0; i<level.length;i++){
if(player.getRect(this).intersects(level[i])){
if(xVel>0){
player.x = level[i].left-player.width/2;
}
if(xVel<0){
player.x = level[i].right+player.width/2;
}
xVel = 0;
}
}
yVel+=grav;
player.y+=yVel;
var jumpable = false;
for(i = 0; i<level.length;i++){
if(player.getRect(this).intersects(level[i])){
if(yVel>0){
player.y = level[i].top-player.height/2;
yVel = 0;
jumpable = true;
}
if(yVel<0){
player.y = level[i].bottom+player.height/2;
yVel*=-0.5;
}
}
}
if(upKeyDown&&jumpable){
jump();
}
this.x = -player.x+(stage.stageWidth/2);
this.y = -player.y+(stage.stageHeight/2);
}
function jump(){
yVel-=jumpHeight;
}
Where I think the problem occurs
player.x+=xVel;
for(i = 0; i<level.length;i++){
if(player.getRect(this).intersects(level[i])){
if(xVel>0){
player.x = level[i].left-player.width/2;
}
if(xVel<0){
player.x = level[i].right+player.width/2;
}
xVel = 0;
}
}
yVel+=grav;
player.y+=yVel;
var jumpable = false;
for(i = 0; i<level.length;i++){
if(player.getRect(this).intersects(level[i])){
if(yVel>0){
player.y = level[i].top-player.height/2;
yVel = 0;
jumpable = true;
}
if(yVel<0){
player.y = level[i].bottom+player.height/2;
yVel*=-0.5;
}
}
}
The picture!
Extra information:
The platforms are movie clip symbols, all originating from the same symbol. They are dragged onto the canvas and re sized. The platform symbol has an AS linkage called 'platform' which is how the child is identified as a platform in the code
The player is a rectangle with no animations
Both the platforms and player have an orientation in the center of the object.
What you need to to is test the ceiling collision first and adjust accordingly before the horizontal check. If you don't, then you player is colliding with the bottom of the platform when you do the horizontal check.

How do I get velocity for my character in as3

im trying to make a teleporter game and my character needs to have some velocity and gravity, does anyone know what sums i need to be able to acomplish this?
This is my code so far:
var char = this.addChild(new Char());
char.width = 20;
char.height = 20;
char.x = startPos.x; //startPos is an invisible movieclip that I can move around to make the starting position
char.y = startPos.y; // accurate
var vx:Number = 0;
var vt:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyDownHandler (e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.UP:
char.y = char.y - 5
}
}
If your char only needs to go up then the following code will do the job.
But if it needs to move in all direction then much advanced code is required.
Follow Moving Character in all directions.
This is a quick solution to your need.
var gravity:Number = 2;
var velocity:Number = 1.1;
var move:Boolean = false;
function moveChar(e:Event):void
{
if(move)
{
gravity *= velocity;
char.y -= gravity; // move char
}
}
char.addEventListener(Event.ENTER_FRAME, moveChar, false, 0, true);
//Keyboard events
function keyDownHandler (e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.UP:
move = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyUpHandler (e:KeyboardEvent):void
{
move = false;
gravity = 2;
}
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

How Do I spawn Enemy at either the far left or the far right of the stage and have them move in to the center?

First I get This Error
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Game_fla::MainTimeline/testCollisions()[Game_fla.MainTimeline::frame1:205]
and here is my code
// ******* IMPORTS *****
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
//*****VARIABLES****
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var shootDown:Boolean = false;
var ySpeed:int = 0;
var xSpeed:int = 0;
var scrollX:int = 0;
var scrollY:int = 0;
var speedConstant:int = 5;
var friction:Number = 0.6;
var level:Number = 1;
var bullets:Array = new Array();
var container_mc:MovieClip;
var enemies:Array;
var tempEnemy:MovieClip;
// BUTTON EVENTS EITHER CLICKED OR NOT
left_btn.addEventListener(MouseEvent.MOUSE_DOWN, moveLeft);
right_btn.addEventListener(MouseEvent.MOUSE_DOWN, moveRight);
up_btn.addEventListener(MouseEvent.MOUSE_DOWN, moveUp);
shoot_btn.addEventListener(MouseEvent.MOUSE_DOWN, shootPressed);
left_btn.addEventListener(MouseEvent.MOUSE_UP, leftUp);
right_btn.addEventListener(MouseEvent.MOUSE_UP, rightUp);
up_btn.addEventListener(MouseEvent.MOUSE_UP, upUp);
stage.addEventListener(Event.ENTER_FRAME, makeEnemies);
player.gotoAndStop('still');
stage.addEventListener(Event.ENTER_FRAME,onenter);
function onenter(e:Event):void{
if (rightPressed == true && leftPressed == false){
player.x += 8;
player.scaleX = 1;
player.gotoAndStop("walking");
cloud.x -= 8;
} else if (leftPressed == true && rightPressed == false){
player.x -= 8;
player.scaleX = -1;
player.gotoAndStop('walking');
cloud.x += 8;
} else if(upPressed == true && leftPressed == false && rightPressed == false){
}
else{
rightPressed = false;
leftPressed = false;
player.gotoAndStop('still')}
}
// **** MOVEMENT CONTROLS *********
function shootPressed(e:MouseEvent):void{
shootDown = true;
if(shootDown == true){
fireBullet();
}
}
function fireBullet():void
{
var playerDirection:String;
if(player.scaleX < 0){
playerDirection = "left";
} else if(player.scaleX > 0){
playerDirection = "right";
}
var bullet:Bullet = new Bullet(player.x, player.y, playerDirection);
//bullets = new Array();
bullet.y = player.y + 8;
stage.addChild(bullet);
bullets.push(bullet);
trace(bullets);
}
// BUTTON FUNCTIONS
function moveLeft(e:MouseEvent):void
{
if(MouseEvent.MOUSE_DOWN){
leftPressed = true;
}else if (MouseEvent.MOUSE_UP) {
leftPressed = false;
}
}
function moveRight(e:MouseEvent):void
{
if (MouseEvent.MOUSE_DOWN){
rightPressed = true;
}else if (MouseEvent.MOUSE_UP){
rightPressed = false;
}
}
function moveUp(e:MouseEvent):void
{
if(MouseEvent.MOUSE_DOWN){
upPressed = true;
} else if (MouseEvent.MOUSE_UP) {
upPressed = false;
}
}
function leftUp(e:MouseEvent):void
{
leftPressed = false;
}
function rightUp(e:MouseEvent):void
{
rightPressed = false;
}
function upUp(e:MouseEvent):void
{
upPressed = false;
}
enemies = new Array();
//Call this function for how many enemies you want to make...
function makeEnemies(e:Event):void
{
var chance:Number = Math.floor(Math.random() * 60);
if (chance <= 2){
//Make sure a Library item linkage is set to Enemy...
tempEnemy = new enemy();
tempEnemy.speed = 80;
tempEnemy.x = Math.round(Math.random() * stage.stageWidth) * -10;
addChild(tempEnemy);
enemies.push(tempEnemy);
moveEnemies();
}
}
function moveEnemies():void
{
var tempEnemy:MovieClip;
for (var i:int =enemies.length-1; i>=0; i--)
{
tempEnemy = enemies[i];
tempEnemy.x += tempEnemy.speed;
tempEnemy.y = 285;
}
}
stage.addEventListener(Event.ENTER_FRAME, testCollisions);
//Check for collisions between an enemies array and a Lasers array
function testCollisions(e:Event):void
{
var tempEnemy:MovieClip;
var tempLaser:MovieClip;
for (var i:int=enemies.length-1; i >= 0; i--)
{
tempEnemy = enemies[i];
for (var j:int=bullets.length-1; j>=0; j--)
{
tempLaser = bullets[j];
if (tempLaser.hitTestObject(tempEnemy))
{
removeChild(tempEnemy);
removeChild(tempLaser);
trace("BULLET HIT");
stage.addEventListener(Event.ENTER_FRAME, testCollisions);
}
}
}
}
I Understand that I need to reference the parent whenever I removeChild in the testCollision function but I dont know where.
Also I want the zombies to spawn out of the stage and move in towards the center at a smooth speed with the code I have they just seem to spawn sort of rearly and always to the left of the stage. So I would need to spawn them off the stage and have them move in to the center and change their ScaleX position to change their dirention but I dont know how to do that Please help.
I think you can fix the error you listed by changing removeChild(tempLaser) to stage.removeChild(tempLaser) since the stage is where you added your bullets, so that's where you need to remove them from.
I'll give you a hint on the zombie movement, but you'll probably want to find a programming forum/friend/professor to help with general code design questions like this. In moveEnemies, you'll need to decide whether the zombie should move left/right (based on whether their x position is larger or smaller than the player's), and whether they should move up/down (based on whether their y position is larger or smaller than the player's).
For example, if their x position is larger than the player's, you would do tempEnemy.x -= tempEnemy.speed, and if smaller, you would do tempEnemy.x += tempEnemy.speed. But as I said, this site isn't really made for these types of design questions.