Copy link to clipboard
Copied
This is the code that I've used. My issue is that, the character will stop in the near center of the screen without moving to the right, before touching the wall. the left and upper walls are fine, however. I need the character to have free movement other then wall colision. This is my final project for class and I'm ahead of scheduale because i want to laze around later.
My character is "mc_glo"
The wall is called "mc_wall1"
the collectables are "mc_scout1" and "mc_zombie1"
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=10;
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 (!mc_wall1.hitTestPoint(mc_glo.x,mc_glo.y-10,true)) {
mc_glo.y-=speed;
}
}
if (movedown==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x,mc_glo.y+10,true)) {
mc_glo.y+=speed;
}
}
if (moveleft==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x,mc_glo.x-10,true)) {
mc_glo.x-=speed;
}
}
if (moveright==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x,mc_glo.x+10,true)) {
mc_glo.x+=speed;
}
}
pickUp();
}
function pickUp(){
if (mc_glo.hitTestObject(mc_scout1)){
mc_scout1.x=1000;
}
if (mc_glo.hitTestObject(mc_zombie1)){
mc_zombie1.x=1000;
}
}
[Moved from non-technical Lounge Forum to specific Program forum... Mod]
[Here is the list of all Adobe forums... https://forums.adobe.com/welcome]
you have typos in
if (moveleft==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x,mc_glo.x-10,true)) {
mc_glo.x-=speed;
}
}
if (moveright==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x,mc_glo.x+10,true)) {
mc_glo.x+=speed;
}
}
that looks like it should be
if (moveleft==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x-10,mc_glo.y,true)) {
mc_glo.x-=speed;
}
}
if (moveright==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x+10,mc_glo.y,true)) {
mc_glo.x+=speed;
}
}
Copy link to clipboard
Copied
you have typos in
if (moveleft==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x,mc_glo.x-10,true)) {
mc_glo.x-=speed;
}
}
if (moveright==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x,mc_glo.x+10,true)) {
mc_glo.x+=speed;
}
}
that looks like it should be
if (moveleft==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x-10,mc_glo.y,true)) {
mc_glo.x-=speed;
}
}
if (moveright==true) {
if (!mc_wall1.hitTestPoint(mc_glo.x+10,mc_glo.y,true)) {
mc_glo.x+=speed;
}
}
Copy link to clipboard
Copied
YOU ARE A GOD-SENT!!
THANK YOU! ❤️ ❤️ ❤️
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
i have one last question actually, if you don't mind--I'm trying to create a "level 2" maze after the character "mc_glo" hits "mc_scout." I copied the previous code for the working maze and pasted it for a level 2 maze, but I'm stormed with numerous errors. I don't think this is the right way to code a second page. How would I properly go about coding a level 2 environment for a maze?
this is the fixed code i copied and pasted into the second level:
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 (!mc_wall2.hitTestPoint(mc_glo.x,mc_glo.y-5,true)) {
mc_glo.y-=speed;
}
}
if (movedown==true) {
if (!mc_wall2.hitTestPoint(mc_glo.x,mc_glo.y+50,true)) {
mc_glo.y+=speed;
}
}
if (moveleft==true) {
if (!mc_wall2.hitTestPoint(mc_glo.x-5,mc_glo.y,true)) {
mc_glo.x-=speed;
}
}
if (moveright==true) {
if (!mc_wall2.hitTestPoint(mc_glo.x+35,mc_glo.y,true)) {
mc_glo.x+=speed;
}
}
pickUp();
}
function pickUp(){
if (mc_glo.hitTestObject(mc_scout1)){
mc_scout1.x=1000;
mc_scout1.addEventListener(KeyboardEvent.KEY_DOWN, fl_ClickToGoToAndStopAtFrame_16);
gotoAndStop(105);
}
if (mc_glo.hitTestObject(mc_zombie2)){
mc_zombie2.x=1000;
mc_zombie2.addEventListener(KeyboardEvent.KEY_DOWN, fl_ClickToGoToAndStopAtFrame_16);
gotoAndStop(82);
}
}
Any input is appreciated.
Copy link to clipboard
Copied
don't copy any of that code. just make the code for level1 more general so it can handle all your other levels. eg,
leave the key listeners alone.
change your gameloop to use mc_wall instead of mc_wall1.
change pickUp to use mc_scout instead of mc_scout1
on the keyframe that displays your first level use,
var mc_wall:MovieClip =mc_wall1;
var mc_scout:MovieClip=mc_scout1;
on the keyframe that displays your 2nd level use,
mc_wall = mc_wall2;
mc_scout = mc_scout2
use an if-statement in pickUp to goto different keyframes for the 'next' level based on the value of mc_scout.
Copy link to clipboard
Copied
Okay. Thank you so much for helping me again btw. I'm going to try this out right now
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
This is the code for level 1 of the maze, but now my character "mc_glo" doesn't move, so i can't go about testing it.
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkkeysdown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkkeysup);
//Recently added.
var mc_wall:MovieClip =mc_wall1;
var mc_scout:MovieClip =mc_scout1;
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 (!mc_wall.hitTestPoint(mc_glo.x,mc_glo.y-5,true)) {
mc_glo.y-=speed;
}
}
if (movedown==true) {
if (!mc_wall.hitTestPoint(mc_glo.x,mc_glo.y+50,true)) {
mc_glo.y+=speed;
}
}
if (moveleft==true) {
if (!mc_wall.hitTestPoint(mc_glo.x-5,mc_glo.y,true)) {
mc_glo.x-=speed;
}
}
if (moveright==true) {
if (!mc_wall.hitTestPoint(mc_glo.x+35,mc_glo.y,true)) {
mc_glo.x+=speed;
}
}
pickUp();
}
function pickUp(){
if (mc_glo.hitTestObject(mc_scout)){
mc_scout.x=1000;
mc_scout.addEventListener(KeyboardEvent.KEY_DOWN, fl_ClickToGoToAndStopAtFrame_16);
gotoAndStop(105);
}
if (mc_glo.hitTestObject(mc_zombie1)){
mc_zombie1.x=1000;
mc_zombie1.addEventListener(KeyboardEvent.KEY_DOWN, fl_ClickToGoToAndStopAtFrame_16);
gotoAndStop(82);
}
}
This is the code for the level 2. "btn_nav" is just the navigator button to this maze.
btn_nav.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_25);
function fl_ClickToGoToAndStopAtFrame_25(event:MouseEvent):void
{
gotoAndStop(108);
}
mc_wall = mc_wall2;
mc_scout = mc_scout2
Copy link to clipboard
Copied
do you see an error? eg, mc_wall2 and mc_scout2 aren't defined?
Copy link to clipboard
Copied
no error messages. I feel there should be.
Copy link to clipboard
Copied
i also named the second level wall "mc_wall2" and the second collectable "mc_scout2"
Copy link to clipboard
Copied
do they both exist on the frame that has gotoAndStop(108) or do they exist on frame 108?
Copy link to clipboard
Copied
The "gotoAndStop (108)" is on the second level and is actually frame 107.
Copy link to clipboard
Copied
so both mc_wall2 and mc_scout2 exist in the frame that contains
mc_wall = mc_wall2;
mc_scout = mc_scout2
if so, there won't be an error.
use the trace function and say your right arrow key to see if there's always a positive hittest with mc_wall1 when you start.
Copy link to clipboard
Copied
I'm still fairly new to coding lingo. What is "Trace function?" How do I ..?
Copy link to clipboard
Copied
1. How do I debug ActionScript that triggers an error message?
http://forums.adobe.com/docs/DOC-2542
2. How do i debug ActionScript that triggers no error message?
Copy link to clipboard
Copied
Perhaps I over complicated my project. I'll just stick with what I have, and come back to this project another time. Again, thank you for time. My apologies for the very late reply.
Copy link to clipboard
Copied
you're welcome.