-
How to change image on key press
I have recently made myself a flash snake game, and for the snake head i have made a pacman which opens and closes, but now i need to make it so that if i were to click left, the pacman image turns left, and if i were to click down, it faces down while going down.
I am new to all of this and can not do it at all, help would be really appreciated!
Code Below:
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
import flash.geom.Point;
import flash.display.Stage;
var score:int;
score = -10;
stage.focus=stage; //This is coding to make the arrow keys work on the gamescreen straight away with no clicking
// constants
const gridSize:int = 20;
const leftWall:int =100; //There Dimensions for when the snake dies on the left wall i have changed these to fit around the border of my image and to make it suitable
const rightWall:int = 580; //Dimensions for when the snake dies on the left wall i have changed these to fit around the border of my image and to make it suitable
const topWall:int = 75; //Dimensions for when the snake dies on the left wall i have changed these to fit around the border of my image and to make it suitable
const bottomWall:int = 385; //Dimensions for when the snake dies on the left wall i have changed these to fit around the border of my image and to make it suitable
const startSpeed:int = 200; //Dimensions for when the snake dies on the left wall i have changed these to fit around the border of my image and to make it suitable
const startPoint:Point = new Point(260,180);
// game state
var gameSprite:Sprite;
var food:Food = new Food();
var gameTimer:Timer;
var snakeParts:Array;
// snake velocity
var snakeMoveX:Number = 0;
var snakeMoveY:Number = 0;
var nextMoveX:Number = 1;
var nextMoveY:Number = 0;
// create game sprite
gameSprite = new Sprite();
addChild(gameSprite);
// create first part of snake
snakeParts = new Array();
var firstSnakePart = new SnakeHead(); //I have changed this to make two different parts to the snake so that it fits to my plans of the game
firstSnakePart.x = startPoint.x;
firstSnakePart.y = startPoint.y;
snakeParts.push(firstSnakePart);
gameSprite.addChild(firstSnakePart);
// create first food
gameSprite.addChild(food);
placeFood();
// set up listener and timer
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyD ownFunction);
gameTimer = new Timer(startSpeed);
gameTimer.addEventListener(TimerEvent.TIMER,moveSn ake);
gameTimer.start();
// register key presses
function keyDownFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
if (snakeMoveX != 1) {
nextMoveX = -1;
nextMoveY = 0;
}
} else if (event.keyCode == 39) {
if (snakeMoveX != -1) {
nextMoveX = 1;
nextMoveY = 0;
}
} else if (event.keyCode == 38) {
if (snakeMoveY != 1) {
nextMoveY = -1;
nextMoveX = 0;
}
} else if (event.keyCode == 40) {
if (snakeMoveY != -1) {
nextMoveY = 1;
nextMoveX = 0;
}
}
}
// move snake one space in proper direction
function moveSnake(event:TimerEvent) {
snakeMoveX = nextMoveX;
snakeMoveY = nextMoveY;
var newX:Number = snakeParts[0].x + snakeMoveX*gridSize;
var newY:Number = snakeParts[0].y + snakeMoveY*gridSize;
// check for collision
if ((newX > rightWall) || (newX < leftWall) || (newY > bottomWall) || (newY < topWall)) {
gameOver();
} else if (hitTail()) {
gameOver();
} else {
// check for food
if ((newX == food.x) && (newY == food.y)) { // This is placed into the game to check if any food is on the GameScreen, if there isn't then it will spawn the food.
placeFood();
newSnakePart();
gameTimer.delay -= 2;
}
placeTail();
snakeParts[0].x = newX;
snakeParts[0].y = newY;
}
}
// randomly place the food
function placeFood() {
score = score + 10; //This adds 10 points for every piece of food being picked up
scoreField.text = score.toString();
var startPoint: int ;
startPoint = 80; //This makes the food spawn at least 80 pixels into the stage and around the stage meaning it won't go outside the borders
var foodX:int = Math.floor(Math.random()* ( rightWall-leftWall)/gridSize)*gridSize + startPoint;
var foodY:int = Math.floor(Math.random()* ( bottomWall-topWall)/gridSize)*gridSize + startPoint;
food.x = foodX;
food.y = foodY;
}
// add one sprite to snake
function newSnakePart() {
var newPart:SnakePart = new SnakePart();
gameSprite.addChild(newPart);
snakeParts.push(newPart);
var mySound:Sound = new foodSound(); //Adds sound to the game when food spawns and the sound is a mario sound of picking up coins and plays when the food respawns
mySound.play(); //Just a instruction to make my sound play
}
// place all parts of snake
function placeTail() {
for(var i:int=snakeParts.length-1;i>0;i--) {
snakeParts[i].x = snakeParts[i-1].x;
snakeParts[i].y = snakeParts[i-1].y;
}
}
// see if the head hit any part of the tail
function hitTail() {
for(var i:int=1;i<snakeParts.length;i++) {
if ((snakeParts[0].x == snakeParts[i].x) && (snakeParts[0].y == snakeParts[i].y)) {
return true;
}
}
return false;
}
// stop game
function gameOver() {
stage.removeEventListener(KeyboardEvent.KEY_DOWN,k eyDownFunction);
gotoAndStop (3); //Once the user has died on the game, then this will take them to the end frame which is the third frame
gameTimer.stop();
removeChild (gameSprite) ; //This was added so that when the game does restart no food was shown from the previous games and doesn't cause any distractions
}
-
.
Hi,
Thats some code there and what you require could be pretty lenghty to go through, so try here, it will / should give you an idea http://gregathons.com/2008/06/13/rpg...vement-part-1/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|