View Full Version : Can't stop my character from walking through objects.
LittleFenris
September 1st, 2006, 11:52 AM
Here is what I am working on:
http://www.littlefenris.com/v2/
My problem is the objects that are meant to stop the character from walking (the yellow blocks) only stop the character if you hit them from the top or bottom when walking in a diagonal direction. If you are walking straight left or right they stop the character just fine but if you hit the blocks while walking diagonally left or right the character walks through the object. If someone could help me figure this out (probably simple for someone w/ more AS experience than me) I would be eternally grateful.
var fenrisDirection = "down";
var speed = 4;
var totalHitSpots = 3;
//SCENE CONTROLS
scene_mc.onEnterFrame = function() {
if (Key.isDown(Key.RIGHT)) {
fenrisDirection = "right";
this._x -= speed;
foreGround_mc._x -= speed;
}
if (Key.isDown(Key.LEFT)) {
fenrisDirection = "left";
this._x += speed;
foreGround_mc._x += speed;
}
if (Key.isDown(Key.UP)) {
fenrisDirection = "up";
this._y += speed;
foreGround_mc._y += speed;
}
if (Key.isDown(Key.DOWN)) {
fenrisDirection = "down";
this._y -= speed;
foreGround_mc._y -= speed;
}
};
fenris_mc.onEnterFrame = function() {
if (fenrisDirection == "right") {
this.gotoAndStop("right");
}
if (fenrisDirection == "left") {
this.gotoAndStop("left");
}
if (fenrisDirection == "up") {
this.gotoAndStop("up");
}
if (fenrisDirection == "down") {
this.gotoAndStop("down");
}
//STOP FENRIS FROM GOING OVER OBJECT
for(i = 1; i <= totalHitSpots; i++){
if ((fenrisDirection == "right") && (this.fenri****Area_mc.hitTest(scene_mc["hitSpot"+i+"_mc"]))) {
scene_mc._x += speed;
foreGround_mc._x += speed;
}
if ((fenrisDirection == "left") && (this.fenri****Area_mc.hitTest(scene_mc["hitSpot"+i+"_mc"]))) {
scene_mc._x -= speed;
foreGround_mc._x -= speed;
}
if ((fenrisDirection == "down") && (this.fenri****Area_mc.hitTest(scene_mc["hitSpot"+i+"_mc"]))) {
scene_mc._y += speed;
foreGround_mc._y += speed;
}
if ((fenrisDirection == "up") && (this.fenri****Area_mc.hitTest(scene_mc["hitSpot"+i+"_mc"]))) {
scene_mc._y -= speed;
foreGround_mc._y -= speed;
}
//scene_mc["hitSpot"+i+"_mc"]._visible = false;
}
}
this.onEnterFrame = function() {
trace(fenrisDirection);
};
Again, my problem is when the character is walking in a diagonal direction and hits the yellow spots from their left or right sides. You can walk from the bottom or top diagonally and it stops the character just fine.
Thanks in advance for any help on this!
InfestedDemon
September 1st, 2006, 12:04 PM
collision detection
LittleFenris
September 1st, 2006, 12:12 PM
collision detection
I guess actually reading my post was too much work?
I'm already using hitTest for collision detection but it doesn't stop the character from walking through the objects if the character is walking diagonally towards the sides of the objects.
InfestedDemon
September 1st, 2006, 12:31 PM
I read it but the HitTest is the problem. I didn't read it all but like...do you have hittest for upper-left, upper-right, lower-left a lower-right movement.
LittleFenris
September 1st, 2006, 12:47 PM
I read it but the HitTest is the problem. I didn't read it all but like...do you have hittest for upper-left, upper-right, lower-left a lower-right movement.
I can't seem to get the variable to change when I'm pressing 2 keys at the same time. I had code in there to set the fenrisDirection to "upright", etc...but when I traced the variable during testing it just stayed up or right and wouldn't change if both keys were pressed. Any ideas why?
I was using this:
if (Key.isDown(Key.RIGHT) && Key.isDown(Key.UP)) {
fenrisDirection = "upright";
}
Problem is when I test it the variable only shows up or right even if both keys are pressed.
LittleFenris
September 1st, 2006, 02:39 PM
OK, I figured it out. I just had to use !Keyis.Down and have all the key combos so he couldn't walk through it diagonally.
Thanks for the help..your comment turned the light on in my brain to figure this out.
Templarian
September 1st, 2006, 04:16 PM
}else if(){
This may help with the multiple keys down.... you solved it but should get use to also doing that.
LittleFenris
September 1st, 2006, 04:21 PM
}else if(){
This may help with the multiple keys down.... you solved it but should get use to also doing that.
Thanks for the advice. That fixed one last issue I was having w/ the character still walking through the object if I pressed down, left and right at the same time. With the "else if" in there it doesn't do that anymore. What exactly does adding the "else if" instead of just "if" do?
superiority
September 2nd, 2006, 10:28 AM
It works like this:
if (bill_loves_jane == true) {
bill_kisses_jane = true;
} else if (bill_likes_jane == true) {
bill_hugs_jane = true;
}
does that make sense. It basically says that if this isn't true, but this is then do this.
Lol, my anagrams are dumb =P
nathan99
September 2nd, 2006, 10:37 AM
Ok. Complete syntax tutorial:
http://www.n99creations.com/?pID=tutorials&col=Blue&tut=basics_of_actionscript_for_flash&p=1&l=Flash_MX_04
If/else centralised snippet(basically the same, its mine too, except Kirupa's changed a tad)
http://www.kirupa.com/developer/actionscript/ifelse.htm
LittleFenris
September 2nd, 2006, 02:48 PM
Ok. Complete syntax tutorial:
http://www.n99creations.com/?pID=tutorials&col=Blue&tut=basics_of_actionscript_for_flash&p=1&l=Flash_MX_04
If/else centralised snippet(basically the same, its mine too, except Kirupa's changed a tad)
http://www.kirupa.com/developer/actionscript/ifelse.htm
Thanks for the links.
I'm trying to get my character to do something if he's standing still for a certain amount of time. It works right as the movie is loaded...if you move around it doesnt happen but once you stand still for a few seconds it changes to the "waiting" frame of the character but then if I move again he instantly goes back to the standing frame right when you stop. I want it to wait that certain time before it goes to the "waiting" frame.
Basically I want something like what happens in Sonic when he stands still too long and he starts tapping his foot.
setInterval(function () {
_root.onEnterFrame = function() {
if (standing == 1) {
fenris_mc.gotoAndStop("waiting");
} else {
standing = 0;
}
};
}, 3000);
I don't understand setInterval fully so I'm sure thats the issue.
A Weird Kid
September 2nd, 2006, 03:23 PM
er, what exactly is there to do in it besides moving around aimlessly
:kommie:
LittleFenris
September 2nd, 2006, 03:58 PM
er, what exactly is there to do in it besides moving around aimlessly
:kommie:
This is just the beginning of a portfolio site for myself. I want to get all the moving and technical stuff done first before I start adding the content and fancy graphics (all the graphics in it now are just placeholders to get the code working properly). Basically you will be able to walk about and explore the little Wolfs area to navigate to the various parts of my site like "galleries" and "about me" and all that. I'll have a map and signs to show the user where to walk for each section plus they will simply be able to click on a map and click a section and it will bring that section up w/o having to walk there for lazy people. Right now all you do is walk around aimlessly as you said. :thumb2:
nathan99
September 2nd, 2006, 08:56 PM
Basically the setInterval function works like this;
var myTimer = setInterval(function_after_delay, delay);
myTimer is the name of the variable which will store the interval information. The myTimer variable isn't always needed, but it is generally good practise to have it there, so you can clear the timer at any point you wish. If you don't clear an interval before setting it's delay again, the timer will run upon itself and you would have your function that is run after the delay executing multiple times when you dont want it to. To stop this you need to use the clearInterval() function. The clearInterval wiped your whole interval based on which variable you use. clearInterval is set up as follows;
clearInterval(variable);
Thus, in my example I would have;
clearInterval(myTimer);
function_after_delay is what (function (http://www.n99creations.com/?pID=tutorials&col=Blue&tut=basics_of_actionscript_for_flash&p=16&l=Flash_MX_04)) will be called upon when the interval has completed. There are multiple formats in which you can use to set up functions. The following method sets up the function within the setInterval declaration and cannot be called upon from anywhere else.
var myTimer = setInterval(function():Void {
//this will be run
}, delay);
Another method of setting up a function to be declared is to set it up before hand and simply calling upon that function, instead of setting up your function within the setInterval() declaration. Thus;
//This will be called
function functionName():Void {
//this will be run
}
//functionName calls it
var myTimer = setInterval(functionName, delay);
The final part is the delay. This bit is pretty straight-forward. Instead of having delay, you would replace that text with your delay. When you put in a timer though, you have to enter in the time delay in milliseconds. Thus, for example, the following sets up two timers, one for half a second, and another for one second and then deletes both delays after 5 seconds.
var secTimer = setInterval(function():Void {
trace("One Second.");
}, 1000);
var halfTimer = setInterval(function():Void {
trace("Half A Second.");
}, 500);
var clearTimer = setInterval(function():Void {
clearInterval(secTimer);
clearInterval(halfTimer);
clearInterval(clearTimer);
trace("Wiped.");
}, 5000);
You can also set your delay to a variable's value, for example;
var milliSeconds:Number = 1000; // One second
var myTimer = setInterval(function():Void {
//this will be run
}, milliSeconds);
Instead of having to use milliseconds however, you can set up your time out as a variable and then multiply that by 1000, as there are 1000 milliseconds within one second. For example;
var seconds:Number = 1.5; // One and a half seconds
var myTimer = setInterval(function():Void {
//this will be run
}, seconds*1000);
Hope that clears up a little about setInterval() ;)
LittleFenris
September 3rd, 2006, 03:02 PM
Wow, thanks for the extensive explanation, I'll read through this a few times and see if I can get my action working.
Thanks.
hkl
September 6th, 2006, 07:54 PM
sort of off topic (i do apologise).
to nathan: Where did you learn your actionscript? books or just tutorials etc?
sidenote: there is alot of good talent coming out of australia and canada these days :D
unfortunately UK is slacking :asleep:
to LittleFenris: Good luck with the game.
nathan99
September 7th, 2006, 03:05 AM
I didn't read anything.. i don't read.. not my thing thus never have, never will :P I sort of taught myself by playing with stuf and browsing around here :)
LittleFenris
September 8th, 2006, 01:12 PM
I didn't read anything.. i don't read.. not my thing thus never have, never will :P I sort of taught myself by playing with stuf and browsing around here :)
If you "browsed around here" then that means you read. There is no way you could learn AS or any other programming language w/o reading about it. Programming is all about reading and understanding.
nathan99
September 8th, 2006, 09:07 PM
If you "browsed around here" then that means you read. There is no way you could learn AS or any other programming language w/o reading about it. Programming is all about reading and understanding.
If downloading peoples FLA files is reading.. tyhen i read :P
~§~
October 3rd, 2006, 09:48 PM
I can't go inside the tent, my characters legs don't move
LittleFenris
October 3rd, 2006, 10:59 PM
I can't go inside the tent, my characters legs don't move
Yeah, its far from done. I'm working on things little by little when I have time. Probably the last thing I do will be actually animating (frame by frame) the wolf walking and all that. This actually won't be a "game" but a different way of navigating my portfolio site. Everybody has buttons and such for navigation, I wanted the user to be able to explore my wolf's little world and see my different galleries in a new and different way from most of the portfolio sites out there. I also wanted to give props to 2 things I love, cartoons and video games. Thats why I decided to do the game-like navigation and frame by frame animation that everything will have. I'll keep your comment about not being able to go inside the tent in mind as well...maybe that can be the contact area.
Thanks
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.