A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Combining two of Tonypa's Tutorials.

  1. #1
    Custard Topping
    Join Date
    Jan 2010
    Posts
    20

    Unhappy Combining two of Tonypa's Tutorials.

    I tried to combine two of Tony's great tuts.

    As found here
    http://www.tonypa.pri.ee/tbw/tut15.html

    And here.

    http://www.tonypa.pri.ee/tbw/tut06.html

    I'm not sure what I did wrong so if you guys could have a crack at these and try to combine these I will give many kugs and hisses. ;;D

    Regards, Mcnugget.

  2. #2
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    I'm not sure what I did wrong
    I'm neither. This is far not the best way to ask questions.
    You have to explain what's your problem, if it gives you any errors, post them here and so on, and not just ask anyone else to combine it.

  3. #3
    Custard Topping
    Join Date
    Jan 2010
    Posts
    20
    Sure.

    Well here's my code if it helps you.

    Code:
    fscommand("allowscale", false);
    fscommand("allowscale", false);
    // our map is 2-dimensional array
    myMap1 = [[1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 2], [1, 0, 1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 2], [1, 1, 1, 1, 1, 1, 1, 1]];
    myMap2 = [[1, 1, 1, 1, 1, 1, 1, 1], [3, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1, 0, 1], [3, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1]];
    // declare game object that holds info
    game = {tileW:30, tileH:30, currentMap:1};
    // walkable tile
    game.Tile0 = function () { };
    game.Tile0.prototype.walkable = true;
    game.Tile0.prototype.frame = 1;
    // wall tile
    game.Tile1 = function () { };
    game.Tile1.prototype.walkable = false;
    game.Tile1.prototype.frame = 2;
    // door object prototype
    game.Doors = function (newMap, newcharx, newchary) { this.newmap = newMap;this.newcharx = newcharx;this.newchary = newchary;};
    game.Doors.prototype.walkable = true;
    game.Doors.prototype.frame = 3;
    game.Doors.prototype.door = true;
    // door tiles
    // make those tiles from the door object passing newmap, newcharx and newchary
    game.Tile2 = function () { };
    game.Tile2.prototype = new game.Doors(2, 1, 4);
    game.Tile3 = function () { };
    game.Tile3.prototype = new game.Doors(1, 6, 4);
    // declare char object, xtile and ytile are tile where chars center is
    char = {xtile:2, ytile:1, speed:4, width:20, height:20};
    // building the world
    function buildMap(map) {
    	// attach empty mc to hold all the tiles and char
    	_root.attachMovie("empty", "tiles", 1);
    	// attach empty mc to hold background tiles
    	_root.tiles.attachMovie("empty", "back", 0);
    	// declare clip in the game object
    	game.clip = _root.tiles;
    	// get map dimensions
    	var mapWidth = map[0].length;
    	var mapHeight = map.length;
    	// loop to place tiles on stage
    	for (var i = 0; i<mapHeight; ++i) {
    		for (var j = 0; j<mapWidth; ++j) {
    			// name of new tile
    			var name = "t_"+i+"_"+j;
    			// make new tile object in the game
    			game[name] = new game["Tile"+map[i][j]]();
    			if (game[name].walkable) {
    				var clip = game.clip.back;
    			} else {
    				var clip = game.clip;
    			}
    			// calculate depth
    			game[name].depth = i*game.tileH*300+j*game.tileW+1;
    			// attach tile mc and place it
    			clip.attachMovie("tile", name, game[name].depth);
    			clip[name]._x = (j*game.tileW);
    			clip[name]._y = (i*game.tileH);
    			// send tile mc to correct frame
    			clip[name].gotoAndStop(game[name].frame);
    		}
    	}
    	var ob = char;
    	// calculate starting position
    	ob.x = (ob.xtile*game.tileW);
    	ob.y = (ob.ytile*game.tileW);
    	// calculate depth
    	ob.depth = ob.y*300+ob.x+1;
    	// add the character mc
    	game.clip.attachMovie("char", "char", ob.depth);
    	// declare clip in the game object
    	ob.clip = game.clip.char;
    	// place char mc
    	ob.clip._x = ob.x;
    	ob.clip._y = ob.y;
    	ob.clip.gotoAndStop(ob.frame);
    }
    function changeMap(ob) {
    	// change the map
    	var name = "t_"+ob.ytile+"_"+ob.xtile;
    	game.currentMap = game[name].newMap;
    	ob.ytile = game[name].newchary;
    	ob.xtile = game[name].newcharx;
    	// remember which way char is facing
    	ob.frame = ob.clip._currentframe;
    	buildMap(_root["myMap"+game.currentMap]);
    }
    function getMyCorners(x, y, ob) {
    	// find corner points
    	ob.downY = Math.floor((y+ob.height-1)/game.tileH);
    	ob.upY = Math.floor((y-ob.height)/game.tileH);
    	ob.leftX = Math.floor((x-ob.width)/game.tileW);
    	ob.rightX = Math.floor((x+ob.width-1)/game.tileW);
    	// check if they are walls
    	ob.upleft = game["t_"+ob.upY+"_"+ob.leftX].walkable;
    	ob.downleft = game["t_"+ob.downY+"_"+ob.leftX].walkable;
    	ob.upright = game["t_"+ob.upY+"_"+ob.rightX].walkable;
    	ob.downright = game["t_"+ob.downY+"_"+ob.rightX].walkable;
    }
    function moveChar(ob, dirx, diry) {
    	// vertical movement
    	// where are our edges?
    	// first we look for y movement, so x is old
    	getMyCorners(ob.x, ob.y+ob.speed*diry, ob);
    	// move got dammit... and check for collisions.
    	// going up
    	if (diry == -1) {
    		if (ob.upleft and ob.upright) {
    			// no wall in the way, move on
    			ob.y += ob.speed*diry;
    		} else {
    			// hit the wall, place char near the wall
    			ob.y = ob.ytile*game.tileH+ob.height;
    		}
    	}
    	// if going down
    	if (diry == 1) {
    		if (ob.downleft and ob.downright) {
    			ob.y += ob.speed*diry;
    		} else {
    			ob.y = (ob.ytile+1)*game.tileH-ob.height;
    		}
    	}
    	// horisontal movement
    	// changing x with speed and taking old y
    	getMyCorners(ob.x+ob.speed*dirx, ob.y, ob);
    	// if going left
    	if (dirx == -1) {
    		if (ob.downleft and ob.upleft) {
    			ob.x += ob.speed*dirx;
    		} else {
    			ob.x = ob.xtile*game.tileW+ob.width;
    		}
    	}
    	// if going right
    	if (dirx == 1) {
    		if (ob.upright and ob.downright) {
    			ob.x += ob.speed*dirx;
    		} else {
    			ob.x = (ob.xtile+1)*game.tileW-ob.width;
    		}
    	}
    	// update char position
    	ob.clip._x = ob.x;
    	ob.clip._y = ob.y;
    	// face the direction
    	ob.clip.gotoAndStop(dirx+diry*2+3);
    	// calculate the tile where chars center is
    	ob.xtile = Math.floor(ob.clip._x/game.tileW);
    	ob.ytile = Math.floor(ob.clip._y/game.tileH);
    		// calculate depth
    	ob.depth = ob.y*300+ob.x+1;
    	ob.clip.swapDepths(ob.depth);
    	// trace(ob.depth);
    	// trace(game["t_"+ob.ytile+"_"+ob.xtile].depth);
    	return (true);
    	// check for door
    	if (game["t_"+ob.ytile+"_"+ob.xtile].door and ob == _root.char) {
    		// make new map
    		changeMap(ob);
    	}
    	return (true);
    }
    function detectKeys() {
    	var ob = _root.char;
    	var keyPressed = false;
    	if (Key.isDown(Key.RIGHT)) {
    		keyPressed = _root.moveChar(ob, 1, 0);
    	} else if (Key.isDown(Key.LEFT)) {
    		keyPressed = _root.moveChar(ob, -1, 0);
    	} else if (Key.isDown(Key.UP)) {
    		keyPressed = _root.moveChar(ob, 0, -1);
    	} else if (Key.isDown(Key.DOWN)) {
    		keyPressed = _root.moveChar(ob, 0, 1);
    	}
    	// walk animation
    	if (!keyPressed) {
    		ob.clip.char.gotoAndStop(1);
    	} else {
    		ob.clip.char.play();
    	}
    }
    // make the map
    buildMap(_root["myMap"+game.currentMap]);
    stop();
    And I don't get any Actionscript Errors. Just a strange moving character which is stuck on one tile and move up if you press down, and down when you press up.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center