dimanche 28 juin 2015

Linking multiple objects to one coding name

I am trying to make an rpg style game in Flash AS3. I am trying to get my character to stop when he hits objects such as trees and buildings. Is there a way I can link multiple objects together and make the code say something like 'take object1, object2, and object3 and name it multipleobjects'? I have the code set up so that the character stops at one tree but I'm not positive how/if you can combine objects so that he won't go through multiple at one time. Thank you very very much in advanced! Objects: manmc (my character), treer1_MC (the first tree) treer2_MC (second tree) and so on

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;

manmc.gotoAndStop ("Stand Front Frame");

var rightPressed:Boolean = new Boolean(false);
var leftPressed:Boolean = new Boolean(false);
var upPressed:Boolean = new Boolean(false);
var downPressed:Boolean = new Boolean(false);
var manSpeed:Number = 3;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);

function keyDownHandler (keyEvent:KeyboardEvent):void
{
    if(keyEvent.keyCode == Keyboard.RIGHT)
    {
        rightPressed = true;
    }
    else if(keyEvent.keyCode == Keyboard.LEFT)
    {
        leftPressed = true;
    }
    else if(keyEvent.keyCode == Keyboard.UP)
    {
        upPressed = true;
    }
    else if(keyEvent.keyCode == Keyboard.DOWN)
    {
        downPressed = true;
    }
}

function keyUpHandler (keyEvent:KeyboardEvent):void
{
    if(keyEvent.keyCode == Keyboard.RIGHT)
    {
        rightPressed = false;
        manmc.gotoAndStop("Stand Right Frame");
    }
    else if(keyEvent.keyCode == Keyboard.LEFT)
    {
        leftPressed = false;
        manmc.gotoAndStop("Stand Left Frame");
    }
    else if(keyEvent.keyCode == Keyboard.UP)
    {
        upPressed = false;
        manmc.gotoAndStop("Stand Back Frame");
    }
    else if(keyEvent.keyCode == Keyboard.DOWN)
    {
        downPressed = false;
        manmc.gotoAndStop("Stand Front Frame");
    }
}

function gameLoop(loopEvent:Event):void
{
    if(rightPressed)
    {
        if(manmc.x < 1050)
        {
            manmc.x += manSpeed;
        }
        manmc.gotoAndStop("Walk Right Frame");

        if (manmc.hitTestObject(treer1_MC))
    {
        trace("leftHit");
        manmc.x -= 3;
    }
}
    else if(leftPressed)
    {
        if (manmc.x > 145)
        {
            manmc.x -= manSpeed;
        }
        manmc.gotoAndStop("Walk Left Frame");

        if (manmc.hitTestObject(treer1_MC))
    {
        trace("rightHit");
        manmc.x += 3;
    }
} 
      else if(downPressed)
{
    if(manmc.y < 780)
    {
    manmc.y += manSpeed;
    }
    manmc.gotoAndStop("Walk Front Frame");

    if (manmc.hitTestObject(treer1_MC))
    {
        trace("downHit");
        manmc.y -= 3;
    }
}
        else if(upPressed)
{
    if(manmc.y > 145)
    {
    manmc.y -= manSpeed;
    }
    manmc.gotoAndStop("Walk Back Frame");

    if (manmc.hitTestObject(treer1_MC))
    {
        trace("upHit");
        manmc.y += 3;
    }
}
}

Aucun commentaire:

Enregistrer un commentaire