Issue with changeMap function

Subscribe to Issue with changeMap function 6 posts

Sign in to reply


 
avatar for Jaywalker2 Jaywalker2 692 posts
Flag Post

I mostly used tonypa’s excellent guide to create tile-based map-editing code for an RPG. However, I’m having great difficulty with changing maps…
Here’s the bits of code that I think are relevant;


map1 = [[0,0,0,0,0,0],
		[0,1,2,1,2,0],
		[4,3,4,3,4,101],
		[0,1,2,1,2,0],
		[0,0,0,0,0,0]];

map2 = [[0,0,4,3,0,0],
		[0,1,2,1,2,0],
		[202,3,0,0,4,0],
		[0,1,2,1,2,0],
		[0,0,0,0,0,0]];
game.Doors = function (newMap, newherox, newheroy, enterx, entery) {
	this.enterx=enterx;
	this.entery=entery;
	if (enterx == -1 || 1) {
		if (enterx == 1) {
			if (dirx == 1) {
				this.newMap=newMap;
				this.newherox=newherox;
				this.newheroy=newheroy;
			}
		} else {
			if (dirx == -1) {
				this.newMap=newMap;
				this.newherox=newherox;
				this.newheroy=newheroy;
			}
		}
	}
	if (entery == -1 || 1) {
		if (entery == 1) {
			if (diry == 1) {
				this.newMap=newMap;
				this.newherox=newherox;
				this.newheroy=newheroy;
			}
		} else {
			if (diry == -1) {
				this.newMap=newMap;
				this.newherox=newherox;
				this.newheroy=newheroy;
			}
		}
	}
};
game.Doors.prototype.nobarrier=true;
game.Doors.prototype.door=true;
game.Tile101 = function () { };
game.Tile101.prototype = new game.Doors(2, 0, 2, 1, 0);
game.Tile101.prototype.frame = 4;
game.Tile202 = function () { };
game.Tile202.prototype = new game.Doors(1, 6, 2, -1, 0);
game.Tile202.prototype.frame = 5;

function moveHero(ob, dirx, diry) {
	getMyCorners (ob.x, ob.y+ob.speed*diry, ob);
	if (diry == -1) {
		if (ob.upleft && ob.upright) {
			ob.y += ob.speed*diry;
		} else {
			ob.y = ob.ytile*game.tileH+ob.height+1;
			ob.clip.hero.gotoAndStop(1);
		}
	}
	if (diry == 1) {
		if (ob.downleft && ob.downright) {
			ob.y += ob.speed*diry;
		} else {
			ob.y = (ob.ytile+1)*game.tileH-ob.height;
			ob.clip.hero.gotoAndStop(1);
		}
	}
	getMyCorners (ob.x+ob.speed*dirx, ob.y, ob);
	
	if (dirx == 1) {
		if (ob.upright && ob.downright) {
			ob.x += ob.speed*dirx;
		} else {
			ob.x = (ob.xtile+1)*game.tileW-ob.width;
			ob.clip.hero.gotoAndStop(1);
		}
	}
	if (dirx == -1) {
		if (ob.upleft && ob.downleft) {
			ob.x  += ob.speed*dirx;
		} else {
			ob.x = ob.xtile*game.tileW+ob.width;
			ob.clip.hero.gotoAndStop(1);
		}
	}
	
	if(game["t_"+ob.ytile+"_"+ob.xtile].door && ob == _root.hero)
	{
		changeMap(ob);
	}
	
	ob.clip.gotoAndStop(dirx+diry*2+3);
	ob.clip._x = ob.x;
	ob.clip._y = ob.y;
	ob.xtile = Math.floor(ob.x/game.tileW);
	ob.ytile = Math.floor(ob.y/game.tileH);
	return(true);
}
function changeMap(ob) {
	var name = "t_"+ob.ytile+"_"+ob.xtile;
	game.currentMap = game[name].newMap;
	ob.ytile = game[name].newheroy;
	ob.xtile = game[name].newherox;
	ob.frame = ob.clip._currentframe;
	buildMap(_root["map"+game.currentMap]);
}

I’m happy to provide more bits of code if needed, but I think the problem is somewhere in here. dirx and diry have values of either “1” or “-1”, and refer of course to which key is down.

The problem I’m GETTING is, where buildMap function should be getting the second of the map arrays I built and, well, building it, it just creates a blank screen…not sure why.

 
avatar for SuperFalconKick SuperFalconKick 98 posts
Flag Post

You seriously need to update your code. This is AS1 is it not? Not even those few AS2 people could defend this.

 
avatar for Jaywalker2 Jaywalker2 692 posts
Flag Post

Pretty sure it’s AS2…I’m using Flash 8, and don’t have the cash for CS, so I can’t do better. I’ve checked through the code to see which parts were deprecated by the time of Flash 8 while I was writing it, and I didn’t see anything, but maybe I missed them.

 
avatar for Jabor Jabor 11382 posts
Flag Post

The style is horrible. You’re dynamically creating all this stuff, when it makes much more sense to stick it in a class file and quit screwing around with prototypes.

Because it’s clear that you don’t really understand how prototypes work, and writing code that you yourself don’t understand isn’t likely to result in making much headway.

 
avatar for Jaywalker2 Jaywalker2 692 posts
Flag Post

Alright, thanks very much for the advice. I don’t really understand how prototypes work, as it turns out, from a scan of prototype tutorials provided by my old friend google, although I thought that I did. What did I do wrong with prototypes specifically? Any input is gratefully taken.

All the prototypes I’ve used seem to be functioning well, but yeah, I don’t like using them because I don’t fully understand how they work, and I don’t think it suits me.
It is 3 am where I am and I am rather inebriated after a night out so apologies for any typoes, but I couldn’t wait til tomorrow to beg for help…is there any benefit to learning to use prototypes properly at this stage? Also, I was rather impressed by the results from dynamically creating things, personally, which I haven’t tried a lot before, what are the strengths of dynamically creating things, and the strengths of resisting this urge?

Ta for any responses, and thank you for your patience so far with my archaic and inefficient/convoluted/plain idiotic coding attempts.

EDIT: OK, two hours solid of research via Moock et al have shown me exactly why this is a STUPID way to organise these things in AS 2.0-onwards. It’s just horrible to manage. I’m now fairly sure, however, that I DID understand prototypes, but equally sure that I will almost certainly never need to use prototypes again. I’m now re-arranging the whole project as Jabor and SuperFalcolnKick recommended. Thanks again. Yours sleeplessly, Jay…

 
avatar for Jabor Jabor 11382 posts
Flag Post

There’s nothing really “wrong” with messing about with prototypes, but most things you normally do with them you can do cleaner and more understandably with regular class-based inheritance.

It’s one of those things that you shouldn’t really use unless you know why you’re using it and why it might be preferable in one particular case.

Sign in to reply