samedi 27 juin 2015

Object Barriers in AS3?

I am a beginner at coding but I am trying to make a flash rpg type game. I have a barrier around my game and a walking person (manmc). I have been watching videos/reading articles on how to make object barriers but I can not seem to get the coding right without getting errors. I am trying to get my character to stop when he hits a tree. The tree is coded as (treer1_MC). I only tried to code it on the down press but it does nothing and it causes the character to not be able to walk back up.

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");
    }
    else if(leftPressed)
    {
        if (manmc.x > 145)
        {
            manmc.x -= manSpeed;
        }
        manmc.gotoAndStop("Walk Left Frame");
    } 
    else if(downPressed)
    {
        if(manmc.y < 780)
        {
        manmc.y += manSpeed;
        }
        manmc.gotoAndStop("Walk Front Frame");
    }
        if (manmc.hitTest(treer1_MC))
    {
            trace("leftHit");
            manmc._x += 3;

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

This is the coding a tutorial said to use. Any suggestions?

if (manmc.hitTest(treer1_MC))
        trace("leftHit");
        manmc._x += 3;

Aucun commentaire:

Enregistrer un commentaire