The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumYep, me again, asking for more help.

12 5108
P051D3N
offline
P051D3N
25 posts
Nomad

Sorry that I seem to be coming here with a new help request every couple of days, but I have another problem .
I am currently using the code below to move my main character.

var varRight:Boolean = false;
var varLeft:Boolean = false;
var varUp:Boolean = false;
var varDown:Boolean = false;

var xspeed:Number = 5
var yspeed:Number = 5

function checkKeys(event:KeyboardEvent){
if (event.keyCode == 39) {
varRight = true;
}
if (event.keyCode == 38) {
varUp = true;
}
if (event.keyCode == 37) {
varLeft = true;
}
if (event.keyCode == 40) {
varDown = true;
}
}

function keyUps(event:KeyboardEvent) {
if (event.keyCode == 39) {
event.keyCode = 0;
varRight=false;
}
if (event.keyCode == 38) {
event.keyCode = 0;
varUp=false;
}
if (event.keyCode == 37) {
event.keyCode = 0;
varLeft=false;
}
if (event.keyCode == 40) {
event.keyCode = 0;
varDown=false;
}
}

function movement(Event){
if (varRight == true) {
player.x += xspeed;
}
if (varUp == true) {
player.y -= yspeed;
}
if (varLeft == true) {
player.x -= xspeed;
}
if (varDown == true) {
player.y += yspeed;
}
}

stage.addEventListener(Event.ENTER_FRAME, movement);
stage.addEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);


It works really well, feel free to use it if you need to, you just need to name a movie clip &quotlayer".
But, I cannot work out how to make a wall that will not let the player through, do I need to use a different code for this or is there something I can add that will make it work?
  • 12 Replies
PixelSmash
offline
PixelSmash
566 posts
Nomad

You'll need some sort of hittesting for that. The code so far - like you said - works pretty well, but you could try changing it to this:

f (varRight == true) {
movePlayer("x", xspeed);
}
if (varUp == true) {
movePlayer("y", -yspeed);
}
if (varLeft == true) {
movePlayer("x", -xspeed);
}
if (varDown == true) {
movePlayer("y", yspeed);
}

function movePlayer(axis:String, amount:Number){
player[axis] += amount;
if(player.hitTestObject(wall)){
player[axis] -= amount;
}
}

What this does is move the player, check if it hits the wall, and if it does place it back where it was.
There are a million different ways to do this, but this is the first thing that came to mind

Good luck!

P051D3N
offline
P051D3N
25 posts
Nomad

For some reason this code is not working for me. I should have mentioned at the start but it is AS3. My code at the moment is:
var varRight:Boolean = false;
var varLeft:Boolean = false;
var varUp:Boolean = false;
var varDown:Boolean = false;

var xspeed:Number = 3
var yspeed:Number = 3

function checkKeys(event:KeyboardEvent){
if (event.keyCode == 39) {
varRight = true;
}
if (event.keyCode == 38) {
varUp = true;
}
if (event.keyCode == 37) {
varLeft = true;
}
if (event.keyCode == 40) {
varDown = true;
}
}

function keyUps(event:KeyboardEvent) {
if (event.keyCode == 39) {
event.keyCode = 0;
varRight=false;
}
if (event.keyCode == 38) {
event.keyCode = 0;
varUp=false;
}
if (event.keyCode == 37) {
event.keyCode = 0;
varLeft=false;
}
if (event.keyCode == 40) {
event.keyCode = 0;
varDown=false;
}
}

if (varRight == true) {
movePlayer("x", xspeed);
}
if (varUp == true) {
movePlayer("y", -yspeed);
}
if (varLeft == true) {
movePlayer("x", -xspeed);
}
if (varDown == true) {
movePlayer("y", yspeed);
}
function movePlayer(axis:String, amount:Number){
player.x += 5;
if(player.hitTestObject(wall)){
player.x -= 5;
}
}

I wasn't sure wheather you meant me to leave the [axis] and amount parts or change them to what I wan't because I don't really understand the last paragraph. Thanks though

beech
offline
beech
107 posts
Nomad

yes - in the code PixelSmash posted he's set up a 'string' to dynamically call the correlating property - ie. x or y

so you do want to leave the "access operator"( ie. the brackets [] ) in there to use the string parameter passed to the method, that is named 'axis'

P051D3N
offline
P051D3N
25 posts
Nomad

OK, thanks for the clarification beech. I've tried it again and I got no error messeges in the error messeges tab, but in output I got :
ArgumentError: Error #1063: Argument count mismatch on Untitled_fla::MainTimeline/movement(). Expected 2, got 1.
and the player won't move at all. I don't understand :S

P051D3N
offline
P051D3N
25 posts
Nomad

^Forget that^
I had mixed up the code with another game.

The problem I have is that there are no error messages, no output errors, but the player just won't move, I've got it exactly like it was on the second post I posted on this topic. PixelSmash said that there are loads of methods to do what I'm trying to do, maybe one of the others would work better?

beech
offline
beech
107 posts
Nomad

how big is the clip wall?

if, for instance you have a 'wall' clip that 'surrounds' the player, like a box around the scene or something with four walls - the player will 'always' be in contact with the clip because it is searching for the bounding box of the entire instance. if this is the case then you'll want to use a *shapeflag* based hitTestPoint method.

P051D3N
offline
P051D3N
25 posts
Nomad

OK, since that wasn't working, I've edited it myself and came up with this

import mx.effects.Tween;
import mx.events.EventDispatcher;
var varRight:Boolean = false;
var varLeft:Boolean = false;
var varUp:Boolean = false;
var varDown:Boolean = false;
var xspeed:Number = 5
var yspeed:Number = 5
function checkKeys(event:KeyboardEvent){
if (event.keyCode == 39) {
varRight = true;
}
if (event.keyCode == 38) {
varUp = true;
}
if (event.keyCode == 37) {
varLeft = true;
}
if (event.keyCode == 40) {
varDown = true;
}
}
function keyUps(event:KeyboardEvent) {
if (event.keyCode == 39) {
event.keyCode = 0;
varRight=false;
}
if (event.keyCode == 38) {
event.keyCode = 0;
varUp=false;
}
if (event.keyCode == 37) {
event.keyCode = 0;
varLeft=false;
}
if (event.keyCode == 40) {
event.keyCode = 0;
varDown=false;
}
}
function movement(Event){
if (varRight == true) {
if (wall1.x == player.x + 26 && wall1.y == player.y) {
//donothing
}
else(){
stage.removeEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUps);
var playerright:Tween = new Tween(player, "x", Regular, player.x, player.x + 26, 20, false)
playerright.addEventListener(TweenEvent.TWEEN_FINISH, rightfinish)
function rightfinish(e:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);
}
if (varUp == true) {
if (wall1.x == player.x && wall1.y == player.y + 26) {
//donothing
}
else(){
stage.removeEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUps);
var playerup:Tween = new Tween(player, "y", Regular, player.y, player.y + 26, 20, false)
playerup.addEventListener(TweenEvent.TWEEN_FINISH, upfinish)
function upfinish(e:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);
}
if (varLeft == true) {
if (wall1.x == player.x - 26 && wall1.y == player.y) {
//donothing
}
else(){
stage.removeEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUps);
var playerleft:Tween = new Tween(player, "x", Regular, player.x, player.x - 26, 20, false)
playerleft.addEventListener(TweenEvent.TWEEN_FINISH, leftfinish)
function leftfinish(e:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);
}
if (varDown == true) {
if (wall1.x == player.x && wall1.y == player.y - 26) {
//donothing
}
else() {
stage.removeEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUps);
var playerdown:Tween = new Tween(player, "y", Regular, player.y, player.y - 26, 20, false)
playerdown.addEventListener(TweenEvent.TWEEN_FINISH, downfinish)
function downfinish(e:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);
}
}
stage.addEventListener(Event.ENTER_FRAME, movement);
stage.addEventListener(KeyboardEvent.KEY_DOWN , checkKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUps);


my game is tile-based (tiles 26-26), so it should work but my character just sits there, with no errors or anything. I don't know what to do...
beech
offline
beech
107 posts
Nomad

ok - well bud there are quite a few problems in there, so lets take a few steps back. first, the system you have been working on here is activated by the keyboard event listeners - now that can work - but there are going to be other problems as your program grows. so we need to separate the two things, the control and the input. lets focus on input right now:

- really what you want to do is have a system that simply checks a keys state: up or down, whenever you want to, from anywhere - here is a simple class that will do that for you, i'll show you how to use it: Keys Class

place this class in the same folder that your game is in, then import the class into your code, just like we do for the native flash classes, like this:

import Keys;

now, we must 'start it up' and pass it a reference to the stage, so we'll call:

Keys.init(stage);

now its ready to get the state of ANY keystroke, at any time, by calling:

Keys.state("W"

the string literal passed in the call is all you need, it is also case sensitive, so you can use "W" or "w" for different commands - however you must also pass the one you intend to check in the correct case also.

this call 'returns' a Boolean value, ie. true or false - true:if the key is currently depressed, false if not. so you would use it in a condition like so:

if(Keys.state("w&quot){ //do your thing }


------
so once you have the key system in place, you should then use a ENTER_FRAME event (or another loop) to process the movement, rather than the event handlers themselves - next post: the event loop and hittesting

beech
offline
beech
107 posts
Nomad

alrighty - i've taken a moment and set up this example. hitTesting is quite a broad subject, there are many ways to do it, many ways to handle it, many methods, theories, etc. - there's out of the box solutions as well, but here is a basic example of what i *think* you're generally trying to accomplish.

this is a very basic, key movement and hittest method, that is 'shape based' - that is, it works on testing single points against the shapes of the walls, rather than against the entire bounding box - in this way you can change the shapes within the walls mc and it will still test against them - however, the example only uses a 'four point' test, you'll note that corners can overlap using this method.

you'll find the code in the first frame, actions layer, it also uses the Key Class above, so place both files into a folder and open the example from there: KeyMoveHitTest Example

beech
offline
beech
107 posts
Nomad

sorry to post yet again :P

it had come to my attention, that the Key class i whipped out up there was flawed, there are some values which do not have corresponding string representations - for instance: the arrow keys - so i have updated the class to allow for key values that are in both forms: as Strings and as uint - so now you can use:

Keys.state( Keyboard.UP );

and/or

Key.state( 38 );

P051D3N
offline
P051D3N
25 posts
Nomad

Sorry for not replying sooner, I have been VERY busy irl. Anyway, I tried it and thankyousomuch, it works really well. Just one more thing, you know the little grey grid sqaures, how do I change how big they are?

beech
offline
beech
107 posts
Nomad

np - you're welcome

are you referring to the grid on the 'stage' in the IDE?

if so, right-click the artboard and use "Grid>Edit grid" to adjust the block size

Showing 1-12 of 12