Simple 2D game implementation

Started by
7 comments, last by jeteran 16 years, 5 months ago
Hi guys from the net. I want to implement this code to my game:

onClipEvent(load){
    jump=false; scream=false; s=6;
	this.gotoAndStop(1);
	b = this.getBounds(this);
	_root.timerID = setInterval(_root.goTime,10);
	function move(x,y){
		h=false;
		if(!_root.map.hitTest(_x+x+b.xmin,_y+y+b.ymin,true)){
			if(!_root.map.hitTest(_x+x+b.xmax,_y+y+b.ymin,true)){
				if(!_root.map.hitTest(_x+x+b.xmin,_y+y+b.ymax,true)){
					if(!_root.map.hitTest(_x+x+b.xmax,_y+y+b.ymax,true)){
						_root.map._x -= x;
						_root.map._y -= y; 
						h=true;
						} 
					}
				}
			}  
	 return h;
	}
}

onClipEvent(enterFrame){
        falling = move(0,s);
		
      if(Key.isDown(Key.SPACE) && !falling && !jump){
			jump = true; vel = -10;
			_root.jumpSound.gotoAndPlay(2);}
	 if(Key.isDown(Key.LEFT)){move(-s,0); this.play();}
     if(Key.isDown(Key.RIGHT)){move(s,0); this.play();}
			
	if (jump) {
		if(vel <= 10){
			h = move(0,vel-s);
			if(h == false && vel < 0){ vel *= -1;}
			vel++;
		}
		else{
			jump = false;
		}
	}
	if (_root.map._y<-450 && !scream) {scream=true; _root.fallSound.gotoAndPlay(2);}
    if (_root.map._y<-850) {_root.map._x=0;_root.map._y=0;scream=false;}
}

onClipEvent (keyUp) {this.gotoAndStop(1);}

And I don't know what happends, but when I put it on my code, it doesn't work! The character won't detect the collision with the floor, just keep falling, I looked at the instance names and such, but I don't know what else. Anybody can help me? Thank you!
Advertisement
... hmm... well, thats part of the problem of copying code... What language , API etc are you using?
Yeah I know, it's AS from Flash ;)
i never copy code because its like some devine mysticle force wants to stop it from working.... it sounds weird but my code( if its copied) usually only works if its saved try that(do you think its my computer or the language?).
Quote:Original post by zabo
i never copy code because its like some devine mysticle force wants to stop it from working.... it sounds weird but my code( if its copied) usually only works if its saved try that(do you think its my computer or the language?).


Wow that's really strange, I am having that problem too, but even tho i save it, it doesn't work :( I am using other codes too
Don't rely on copy-pasting code from tutorials if you're building a game of your own. There are so many things that can differ that you're only causing yourself trouble. Instead, learn how to program yourself. Once you can program, you can read tutorials, understand the concepts that they present, and implement these concepts yourself.

As for pinpointing problems, you can trace text to the console, which is usefull for testing which code-lines are executed and what values variabeles have. That should be helpfull when tracking down bugs and such.
Create-ivity - a game development blog Mouseover for more information.
Quote:Original post by Captain P
Don't rely on copy-pasting code from tutorials if you're building a game of your own. There are so many things that can differ that you're only causing yourself trouble. Instead, learn how to program yourself. Once you can program, you can read tutorials, understand the concepts that they present, and implement these concepts yourself.

As for pinpointing problems, you can trace text to the console, which is usefull for testing which code-lines are executed and what values variabeles have. That should be helpfull when tracking down bugs and such.


Yeah I know, it's always best to create my own code. But I haven't ever build a console-type game. So I am using a code a guy made so we can use to put more levels and stuff. For now it's working, I am using his level ;);)

Thank you guys!

yayo
Don't know what happened! I was doing great, and suddently, my char stopped moving!! I Ctrl+Z a lot and even tho, it never moved again!!!! HELP! This is the code

onClipEvent(load){
jump=false; scream=false; s=8;
this.gotoAndStop(1);
b = this.getBounds(this);
_root.timerID = setInterval(_root.goTime,10);
function move(x,y){
h=false;
if(!_root.map.hitTest(_x+x+b.xmin,_y+y+b.ymin,true)){
if(!_root.map.hitTest(_x+x+b.xmax,_y+y+b.ymin,true)){
if(!_root.map.hitTest(_x+x+b.xmin,_y+y+b.ymax,true)){
if(!_root.map.hitTest(_x+x+b.xmax,_y+y+b.ymax,true)){
_root.map._x -= x;
_root.map._y -= y;
h=true;
}
}
}
}
return h;
}
}

onClipEvent(enterFrame){
falling = move(0,s);

if(Key.isDown(Key.SPACE) && !falling && !jump){
jump = true; vel = -10;
_root.jumpSound.gotoAndPlay(2);}
if(Key.isDown(Key.LEFT)){move(-s,0); this.play();}
if(Key.isDown(Key.RIGHT)){move(s,0); this.play();}

if (jump) {
if(vel <= 10){
h = move(0,vel-s);
if(h == false && vel < 0){ vel *= -1;}
vel++;
}
else{
jump = false;
}
}
if (_root.map._y<-450 && !scream) {scream=true; _root.fallSound.gotoAndPlay(2);}
if (_root.map._y<-850) {_root.map._x=0;_root.map._y=0;scream=false;}
}

onClipEvent (keyUp) {this.gotoAndStop(1);}

Thank you guys!
WOW ok it was because I changed a sound from the scene, isn't that strange? Why is happening that??
TY

This topic is closed to new replies.

Advertisement