Copy link to clipboard
Copied
So for a simple game I need to remove 1 life/heart when the player collides with the wall.
The first heart works, but the second only should be invisible after the first is already gone, and so on.
How do I state that? I now tried it with if-statements in a row, but that's probably not the way to do it.
And how do I equate with alpha and an array element? Or is there a more straight forward way?
I'd really appreciate your help! I'm still a beginner.
var wallArray:Array = new Array(wall1,wall2,wall3,wall4, wall5,sidewall1, sidewall2, sidewall3, sidewall4);
var heartArray:Array = new Array (heart1, heart2, heart3);
function collisionDetectPlayer(){
for(var i:uint = 0; i < wallArray.length;i++){
var wallTemp:MovieClip = wallArray;
if(player.hitTestObject(wallTemp)){
movePlayerBack();
heartArray[2].alpha = 0;
if ((heartArray[2]) < (alpha = 1)){
heartArray[1].alpha = 0;
if ((heartArray[1]) < (alpha = 1)){
heartArray[0].alpha = 0;
}
}
}
}
}
Copy link to clipboard
Copied
you should use a variable to track the number of lives lost (or remaining) and use that variable in your array.
Copy link to clipboard
Copied
how do you mean? do you have an example?
Copy link to clipboard
Copied
var lives:int=10;
function collisionDetectPlayer(){
for(var i:uint = 0; i < wallArray.length;i++){
var wallTemp:MovieClip = wallArray;
if(player.hitTestObject(wallTemp)){
lives--;
if(lives<=0){
gameOverF();
}
movePlayerBack();
heartArray[lives-1].alpha=0; // or heartArray[heartArray.length-lives+1].alpha=0
}
}
}
p.s. i just noticed your if-statements have a few errors. so, in the future you should use something like
if(heartArray[whatever].alpha<.5){
//do whatever
}
Copy link to clipboard
Copied
okay, thanks. But now it keeps taking lives when standing again a wall (in-game goal is to avoid them).
And everytime I touch the wall I get this error:
It says in dutch: "A term is undefined and doesn't have properties" error 1010. What could be wrong?
Your help is gold to me!
Heres the total code:
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
//Variables
var player:MovieClip = player;
var pDir:String = new String();
var pStepSize:int = 5;
var wallArray:Array = new Array(wall1,wall2,wall3,wall4, wall5, sidewall1, sidewall2, sidewall3, infoWall);
var MoneyBagArray:Array = new Array (MoneyBag1, MoneyBag2, MoneyBag3);
var heartArray:Array = new Array (heart1, heart2, heart3);
var finish:MovieClip = finish;
var gameTimer:Timer = new Timer(0);
var count_time:Number = 40;
var myTimer:Timer = new Timer(1000,count_time);
var countScore_int:int = 0;
var countScore_tf:TextField = new TextField();
var gameOverScreen:MovieClip = gameOverScreen;
var startGameScreen:MovieClip = startGameScreen;
var lives:int=10;
//Init
player.stop();
pDir = "NONE";
countScore_tf.text = "0";
addChild(countScore_tf);
gameOverScreen.visible = false;
endGameScreen.visible = false;
myTimer.stop();
gameTimer.delay = 25
gameTimer.repeatCount = 0;
gameTimer.start();
//Events
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
//Functions
function onTick(e:TimerEvent):void
{
movePlayer();
rotatePlayer();
collisionDetectWall();
collisionDetectFinish();
collisionDetectMoneyBag();
}
function collisionDetectMoneyBag(){
for(var i:uint = 0; i < MoneyBagArray.length;i++){
var MoneyTemp:MovieClip = MoneyBagArray;
if(player.hitTestObject(MoneyBagArray[0])){
trace (MoneyBagArray[0]);
}
}
}
function keyPressedDown(e:KeyboardEvent) {
var key:uint = e.keyCode;
startGameScreen.visible = false;
myTimer.start();
if(key == Keyboard.LEFT){
pDir = "LEFT";
player.play();
}
if(key == Keyboard.RIGHT){
pDir = "RIGHT";
player.play();
}
if(key == Keyboard.UP){
pDir = "UP";
player.play();
}
if(key == Keyboard.DOWN){
pDir = "DOWN";
player.play();
}
}
function keyPressedUp(e:KeyboardEvent) {
var key:uint = e.keyCode;
if(key == Keyboard.LEFT ||
key == Keyboard.RIGHT ||
key == Keyboard.UP ||
key == Keyboard.DOWN){
pDir = "NONE";
player.stop();
}
}
function movePlayer(){
if (pDir == "LEFT"){
player.x -= pStepSize;
}
if (pDir == "RIGHT"){
player.x += pStepSize;
}
if (pDir == "UP"){
player.y -= pStepSize;
}
if (pDir == "DOWN"){
player.y += pStepSize;
}
}
function rotatePlayer(){
if (pDir == "LEFT"){
player.rotation = -180;
}
if (pDir == "RIGHT"){
player.rotation = 0;
}
if (pDir == "UP"){
player.rotation = -90;
}
if (pDir == "DOWN"){
player.rotation = -270;
}
}
function movePlayerBack(){
if (pDir == "LEFT"){
player.x += pStepSize;
}
if (pDir == "RIGHT"){
player.x -= pStepSize;
}
if (pDir == "UP"){
player.y += pStepSize;
}
if (pDir == "DOWN"){
player.y -= pStepSize;
}
}
function collisionDetectWall(){
for(var i:uint = 0; i < wallArray.length;i++){
var wallTemp:MovieClip = wallArray;
if(player.hitTestObject(wallTemp)){
movePlayerBack();
lives--;
if(lives<=0){
gameOverScreen.visible = true;
}
heartArray[lives-1].alpha=0; // or heartArray[heartArray.length-lives+1].alpha=0
}
}
}
function countdown(event:TimerEvent):void {
countdownTime_tf.text = String((count_time) - myTimer.currentCount);
if(myTimer.currentCount == 0){
gameOverScreen.visible = true;
}
}
function collisionDetectFinish(){
if(player.hitTestObject(Finish) == true){
endGameScreen.visible = true;
}
}
Copy link to clipboard
Copied
to start, your movePlayerBack should move the player so he's not longer touching the wall and no longer losing lives.
next, you need to stop checking for collisions when the game is over.
Copy link to clipboard
Copied
about the latter one; that wouldn't look like this would it?
do you mean 'when the game is over' or 'when 1 life is over'?
What would that look like?
Copy link to clipboard
Copied
no.
you should use an end game function that probably needs to do several things including removing that gameTimer listener and adding your game over screen and maybe other things.
function endGameF():void{
gameTimer.removeEventListener(TimerEvent.TIMER, onTick);
gameOverScreen.visible = true;
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now