Pergunta

I am having a bit of trouble with my AS3 code, I have made a simple maze game where you have to collect four pieces of "DNA" and once you have collected them a complete screen pops up saying congratulations.

Please could you help me with the code to make it so that once the user has collected all four DNA objects (with instance names of "DNA1", "DNA2", "DNA3" and "DNA4" I want the "completeScreen" object to move to the location X=512, Y=384.

Any help with this would be greatly appreciated!

I have included my code below. Hopefully you will be able to assist me.

import flash.events.KeyboardEvent;
import flash.events.Event;

stage.addEventListener(KeyboardEvent.KEY_DOWN, checkkeysdown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkkeysup);

var moveup:Boolean=false;
var movedown:Boolean=false;
var moveleft:Boolean=false;
var moveright:Boolean=false;

var speed:Number=5;



function checkkeysdown(mykey:KeyboardEvent) 
{
if (mykey.keyCode==Keyboard.UP)
{ 
moveup=true;
}

if (mykey.keyCode==Keyboard.DOWN)
{ 
movedown=true;
}

if (mykey.keyCode==Keyboard.LEFT)
{ 
moveleft=true;
}

if (mykey.keyCode==Keyboard.RIGHT)
{ 
moveright=true;
}
}




function checkkeysup(mykey:KeyboardEvent) 
{
if (mykey.keyCode==Keyboard.UP)
{ 
moveup=false;
}

if (mykey.keyCode==Keyboard.DOWN)
{ 
movedown=false;
}

if (mykey.keyCode==Keyboard.LEFT)
{ 
moveleft=false;
}

if (mykey.keyCode==Keyboard.RIGHT)
{ 
moveright=false;
}
}




stage.addEventListener(Event.ENTER_FRAME, gameloop);


function gameloop (evt:Event)
{
if (moveup==true) 
{
if (!maze.hitTestPoint(player.x,player.y-5,true))
{
player.y-=speed;    
}
}

if (movedown==true) 
{
if (!maze.hitTestPoint(player.x,player.y+5,true))
{
player.y+=speed;    
}
}

if (moveleft==true) 
{
if (!maze.hitTestPoint(player.x-5,player.y,true))
{
player.x-=speed;    
}
}

if (moveright==true) 
{
if (!maze.hitTestPoint(player.x+5,player.y,true))
{
player.x+=speed;    
}
}




pickUp();
}

function pickUp()
{
if (player.hitTestObject(DNA1))
{
DNA1.x=5000;
}

if (player.hitTestObject(DNA2))
{
DNA2.x=5000;
}

if (player.hitTestObject(DNA3))
{
DNA3.x=5000;
}

if (player.hitTestObject(DNA4))
{
DNA4.x=5000;
}

}
Foi útil?

Solução

Add this to the end of your pickUp function.

if ( DNA1.x==5000 && DNA2.x==5000 && DNA3.x==5000 && DNA4.x==5000){
   completeScreen.x = 512;
   completeScreen.y = 384;
}

This should work, but a better way would probably be to add some Boolean to check agains instead like these.

var DNA1Collected:Boolean = false;
var DNA2Collected:Boolean = false;
var DNA3Collected:Boolean = false;
var DNA4Collected:Boolean = false;

Then as the player collects them you can set them to true. That way down the road if you want to do something besides move the x to -5000 your if statement will still work.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top