Making your 2D game, part 3
Ok first you need to create a single block, don't worry about the size just make it a rectangle.
Make it an movieclip (f8) and have it centre registration.
This is now going to be our wall. It will stop the character in its tracks.
Make it an movieclip (f8) and have it centre registration.
This is now going to be our wall. It will stop the character in its tracks.
Change its Instance Name to wall1. Then on the character's code add:
if(this.hitTest(_root.wall1)==true){
rightspeed = 0; //whateverspeed = 0; means you won't be able to move that direction
}else{
rightspeed = 5; // so when your not hitting it you can still move
}
if(this.hitTest(_root.wall1)==true){
rightspeed = 0; //whateverspeed = 0; means you won't be able to move that direction
}else{
rightspeed = 5; // so when your not hitting it you can still move
}
Doing it this way, keeps all the code together as it will all be on the character, but its a lot of space used up, you can collapse it to seem smaller but it still get very complicated. So the other way to do it is on the block we put practically the same code but with a little variation. (Note only use one of the methods, you don't want to double up code it can do horrible things):
onClipEvent (enterFrame) {
if (this.hitTest(_root.char) == true) {//make sure that your character is called char
_root.char.rightspeed = 0;//we are calling on char's rightspeed not its own so we use _root.char.rightspeed
}else{
_root.char.rightspeed = 5;
}
}
So those are two of the ways you can make walls. And with the second method copy and paste the walls and they will start stopping the character straight away (you may need to adjust which direction you want to stop though and their instance names (they can't be the same)). Also if you don't want 4 walls just to stop moving past one spot you can try either:
onClipEvent(enterFrame){
if(this.hitTest(_root.char)==true){
_root.char._x = 50; //you may need to change these points
_root.char._y = 50;
}
}
or
onClipEvent(enterFrame){
if(this.hitTest(_root.char)==true){
_root.gotoAndStop(2); //the number in the brackets is the frame you want to go to
}//make sure that frame exists as a Key Frame
}
Onto Part 4.
onClipEvent (enterFrame) {
if (this.hitTest(_root.char) == true) {//make sure that your character is called char
_root.char.rightspeed = 0;//we are calling on char's rightspeed not its own so we use _root.char.rightspeed
}else{
_root.char.rightspeed = 5;
}
}
So those are two of the ways you can make walls. And with the second method copy and paste the walls and they will start stopping the character straight away (you may need to adjust which direction you want to stop though and their instance names (they can't be the same)). Also if you don't want 4 walls just to stop moving past one spot you can try either:
onClipEvent(enterFrame){
if(this.hitTest(_root.char)==true){
_root.char._x = 50; //you may need to change these points
_root.char._y = 50;
}
}
or
onClipEvent(enterFrame){
if(this.hitTest(_root.char)==true){
_root.gotoAndStop(2); //the number in the brackets is the frame you want to go to
}//make sure that frame exists as a Key Frame
}
Onto Part 4.