Jeu vidéo flash

Fermé
delizar - 28 déc. 2016 à 16:15
bonjour,j'ai une lacune au niveau d'exécution de ce code sur adobe flash Professionnel CS6,il m'affiche un message d'erreur, voila le message:

Les polices doivent être incorporées pour tous les textes pouvant être modifiés à l'exécution, hormis pour les textes dont le paramètre "Utiliser les polices de périphérique" est activé. Utilisez la commande Texte > Incorporation de polices pour incorporer les polices.
TypeError: Error #1009: Il est impossible d'accéder à la propriété ou à la méthode d'une référence d'objet nul.
at jeusnake_fla::MainTimeline/initialize()
at jeusnake_fla::MainTimeline/frame1()

voici le code actionScript3:

import flash.display.MovieClip;

var container:Sprite;
var score:int = 0;
var headWidth:Number = 25; //width of snake head
var hudHeight:int = 50; //height of the hud placed at bottom of the stage
var columns:Number = stage.stageWidth/headWidth; //total num of columns for grid
var rows:Number = (stage.stageHeight - hudHeight)/headWidth; //total num of rows for grid
var food:Sprite; //holds snake’s food
var foodX:Number; //food’s x position
var foodY:Number; //food’s y position
var foodWidth:Number = headWidth; //same as snake’s head for a sake of game play
var direction:String; //set the direction of the snake
var running:Boolean = false;
var keyFrame:int;
var frameNum:int = 0; //current frame num
var span:int = 20;//num of frames between two keyframes

//Create grid of tiles

function grid()
{
for(var i:int = 0; i < rows; i++){

for(var j:int = 0; j < columns; j++) {
var tile:Sprite = new Tile(); //get tile from the library

//Arrange tiles to form a grid

tile.x = (headWidth/2) + (j * headWidth);

tile.y = (headWidth/2) + (i * headWidth);

addChild(tile);
}
}

}

grid();

var head:MovieClip; //holds snake’s head
var headX:Number; //head’s x position
var headY:Number; //head’s y position
var partsArr:Array = new Array(); //holds Snake’s head and body parts
var gameOver_DiagBox:MovieClip;
//Initialization

function initialize()
{
gameOver_DiagBox.visible = false;

//Get and place snake’s head from the library

container = new MovieClip();
addChild(container);

head = new Head();
addChild(head);

//Push Snake’s head into parts array

partsArr.push(head);

//head’s random position for every new start

headX = Math.ceil(((stage.stageWidth-headWidth)/(headWidth))*Math.random())*headWidth;

headY = 100; //Math.ceil((((stage.stageHeight-hudHeight) – headWidth)/(headWidth))*Math.random())*headWidth;

//Since the head’s pivot is at the centre, adjust its position

var t1:Number = headX - headWidth/2;
var t2:Number = headY - headWidth/2;

head.x = t1;

head.y = t2;

//Get and place food from the library

food = new Food();

container.addChild(food);

//Random position of food

newFood();
}

initialize();

//Place the food at random position
function newFood()
{
foodX = Math.ceil(((stage.stageWidth-foodWidth)/(foodWidth))*Math.random())*foodWidth;
foodY = Math.ceil((((stage.stageHeight-hudHeight)-foodWidth)/(foodWidth))*Math.random())*foodWidth;

food.x = foodX - foodWidth/2;
food.y = foodY - foodWidth/2;

//Find new position each time when food is overlapping snake’s any body part

var i:int;

for (i=partsArr.length-1; i>0; i--)
{

if(food.x == partsArr[i].x && food.y == partsArr[i].y){
newFood();
}
}
}

//Set snake’s direction

function changeDirection(e:KeyboardEvent)
{
if(e.keyCode == 37)
{
if (partsArr.length == 1)
{
direction = "left";
head.rotation = -180;
}
else if (direction != "right")
{
direction = "left";
head.rotation = -180;
}
}
else if (e.keyCode == 38)
{
if (partsArr.length == 1)
{
direction = "up";
head.rotation = -90;
}
else if (direction != "down")
{
direction = "up";
head.rotation = -90;
}

} else if (e.keyCode == 39)
{
if (partsArr.length == 1)
{
direction = "right";
head.rotation = 0;
}
else if (direction != "left")
{
direction = "right";
head.rotation = 0;
}
} else if(e.keyCode == 40)
{
if (partsArr.length == 1)
{
direction = "down";
head.rotation = 90;
}
else if (direction != "up")
{
direction = "down";
head.rotation = 90;
}
}

if(!running)
{
stage.addEventListener(Event.ENTER_FRAME, moveHead);
running = true;
}


}

//Moving snake
function moveHead(e:Event):void
{
keyFrame = frameNum/span;

if(frameNum/span == keyFrame){ //Delay for given time span

if( direction == "left" ){
head.x -= headWidth;

} else if ( direction == "up" )
{
head.y -= headWidth;
}
else if( direction == "right" )
{
head.x += headWidth;
} else if ( direction == "down" )
{
head.y += headWidth;
}

frameNum = 0;
}

//at the time of leaving current position.

if(frameNum == span-1)
{
//Check if snake grabbed the food

restrictSnakeMove();
penetration();

if (head.x == food.x && head.y == food.y)
{
attachNewPart();
newFood();
}

if(running)
{
//Follow the next part
for (var i:int = partsArr.length-1;i > 0; i--)
{
partsArr[i].y = partsArr[(i-1)].y;
partsArr[i].x = partsArr[(i-1)].x;
}
}
}

frameNum++;
}

//Add new part to the snake’s body

function attachNewPart()
{
var newPart:Sprite = new BodyPart();

addChild(newPart);

partsArr.push(newPart); //add new part into snake body
score += 5;

score_TF.text = String(score);
}

//Reatrict snake movements beyond game play area
function restrictSnakeMove()
{

//check for all four boundries of the stage

if (direction == "right" && head.x > (stage.stageWidth - headWidth))
{
dead();
} else if (direction == "left" && head.x < headWidth)
{
dead();
} else if (direction == "down" && head.y > (stage.stageHeight - hudHeight - headWidth))
{
dead();
} else if (direction == "up" && head.y < headWidth)
{
dead();
}
}

//If snake does not obey the game rule he will die
function dead()
{
running = false;

stage.removeEventListener(Event.ENTER_FRAME, moveHead);

stage.removeEventListener(KeyboardEvent.KEY_DOWN, changeDirection);

addChild(gameOver_DiagBox);

gameOver_DiagBox.visible = true;
}

//Check if snake penetrates himself
function penetration()
{
for (var i:int = 1; i < partsArr.length; i++)
{
if(head.x == partsArr[i].x && head.y == partsArr[i].y)
{
dead();
}
}
}

//Restart game
function restartGame(evt:MouseEvent):void
{
score = 0;
score_TF.text = String(score);
frameNum = 0;

for (var i:int = 0; i < partsArr.length; i++)
{
//Remove snake from the stage
removeChild(partsArr[i]);
}

partsArr = [];
container.removeChild(food);
stage.addEventListener(KeyboardEvent.KEY_DOWN, changeDirection);
initialize();
}

gameOver_DiagBox.restart.addEventListener(MouseEvent.CLICK, restartGame);

stage.addEventListener(KeyboardEvent.KEY_DOWN, changeDirection);