- Home
- Categorie
- Digital Marketing
- Grafica, Visual Design & UX
- Scrolling in una sola direzione
-
Scrolling in una sola direzione
Sono alle prese con un piccolo game 2D.
Vorrei modificare lo scrolling del gioco in modo tale che lo faccia solamente per la direzione x. Cioè quando sposto il char in alto e in basso vorrei che le tile rimanessero ferme. Ho provato a modificare e/o eliminare alcune righe di codice, ma nulla da fare.
Allego il file e posto il codice: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, 1], [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], [1, 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, visx:7, visy:5, centerx:120, centery:90};
// 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);
// non-existing tile
game.Tile4 = function () { };
game.Tile4.prototype.walkable = false;
game.Tile4.prototype.frame = 20;
// declare char object, xtile and ytile are tile where chars center is
char = {xtile:1, ytile:2, speed:4};
// building the world
function buildMap(map) {
// attach empty mc to hold all the tiles and char
root.attachMovie("empty", "tiles", 1);
// find number of half the visible tiles
game.halfvisx = int(game.visx/2);
game.halfvisy = int(game.visy/2);
// declare clip in the game object
game.clip = root.tiles;
// place clip in correct coordinates
game.clip.x = game.centerx-(char.xtilegame.tileW)-game.tileW/2;
game.clip._y = game.centery-(char.ytilegame.tileH)-game.tileH/2;
// loop to place tiles on stage
for (var y = char.ytile-game.halfvisy; y<=char.ytile+game.halfvisy+1; ++y) {
for (var x = char.xtile-game.halfvisx; x<=char.xtile+game.halfvisx+1; ++x) {
// name of new tile
var name = "t"+y+""+x;
// check if tile is on the map area
if (y>=0 and x>=0 and y<=map.length-1 and x<=map[0].length-1) {
// make new tile object in the game
game[name] = new game"Tile"+map[y][x];
} else {
// make new empty tile object
game[name] = new game.Tile4();
}
// attach tile mc and place it
game.clip.attachMovie("tile", name, 1+y100+x2);
game.clip[name].x = (xgame.tileW);
game.clip[name]._y = (ygame.tileH);
// send tile mc to correct frame
game.clip[name].gotoAndStop(game[name].frame);
}
}
// add the frame above everything
root.attachMovie("frame", "frame", 100);
// add the character mc
game.clip.attachMovie("char", "char", 10000);
// declare clip in the game object
char.clip = game.clip.char;
// calculate starting position
char.x = (char.xtilegame.tileW)+game.tileW/2;
char.y = (char.ytilegame.tileW)+game.tileW/2;
// add char dimensions to char object, half of clips width and height
char.width = char.clip.width/2;
char.height = char.clip.height/2;
// place char mc
char.clip.x = char.x;
char.clip.y = char.y;
char.clip.gotoAndStop(char.frame);
char.xstep = char.x;
char.ystep = char.y;
}
function changeTile(xold, yold, xnew, ynew, map) {
// name of new tile
var nameold = "t"+yold+""+xold;
var namenew = "t"+ynew+""+xnew;
// check if tile is on the map area
if (ynew>=0 and xnew>=0 and ynew<=map.length-1 and xnew<=map[0].length-1) {
// make new tile object in the game
game[namenew] = new game"Tile"+map[ynew][xnew];
game.clip[nameold].name = namenew;
game.clip[namenew].gotoAndStop(game[namenew].frame);
game.clip[namenew].x = (xnewgame.tileW);
game.clip[namenew]._y = (ynewgame.tileH);
} else {
// make new empty tile object
game[namenew] = new game.Tile4();
game.clip[nameold].name = namenew;
game.clip[namenew].gotoAndStop(game[namenew].frame);
}
}
function changeMap(ob) {
//
var name = "t"+ob.ytile+""+ob.xtile;
game.currentMap = game[name].newMap;
ob.ytile = game[name].newchary;
ob.xtile = game[name].newcharx;
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.speeddiry, 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.speeddiry;
} else {
// hit the wall, place char near the wall
ob.y = ob.ytilegame.tileH+ob.height;
}
}
// if going down
if (diry == 1) {
if (ob.downleft and ob.downright) {
ob.y += ob.speeddiry;
} else {
ob.y = (ob.ytile+1)game.tileH-ob.height;
}
}
// horisontal movement
// changing x with speed and taking old y
getMyCorners(ob.x+ob.speeddirx, ob.y, ob);
// if going left
if (dirx == -1) {
if (ob.downleft and ob.upleft) {
ob.x += ob.speeddirx;
} else {
ob.x = ob.xtilegame.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+diry2+3);
// calculate the tile where chars center is
ob.xtile = Math.floor(ob.x/game.tileW);
ob.ytile = Math.floor(ob.y/game.tileH);
// scroll the background
game.clip.x = game.centerx-ob.x;
game.clip.y = game.centery-ob.y;
// check for door
if (game["t"+ob.ytile+""+ob.xtile].door and ob == _root.char) {
// make new map
changeMap(ob);
}
// check if we have to delete and add rows and columns of tiles
if (ob.xstep<ob.x-game.tileW) {
var xtile = Math.floor(ob.xstep/game.tileW)+1;
var xnew = xtile+game.halfvisx+1;
var xold = xtile-game.halfvisx-1;
for (var i = ob.ytile-game.halfvisy-1; i<=ob.ytile+game.halfvisy+1; ++i) {
changeTile(xold, i, xnew, i, _root["myMap"+game.currentMap]);
}
ob.xstep = ob.xstep+game.tileW;
} else if (ob.xstep>ob.x) {
var xtile = Math.floor(ob.xstep/game.tileW);
var xold = xtile+game.halfvisx+1;
var xnew = xtile-game.halfvisx-1;
for (var i = ob.ytile-game.halfvisy-1; i<=ob.ytile+game.halfvisy+1; ++i) {
changeTile(xold, i, xnew, i, _root["myMap"+game.currentMap]);
}
ob.xstep = ob.xstep-game.tileW;
}
if (ob.ystep<ob.y-game.tileH) {
var ytile = Math.floor(ob.ystep/game.tileH)+1;
var ynew = ytile+game.halfvisy+1;
var yold = ytile-game.halfvisy-1;
for (var i = ob.xtile-game.halfvisx-1; i<=ob.xtile+game.halfvisx+1; ++i) {
changeTile(i, yold, i, ynew, _root["myMap"+game.currentMap]);
}
ob.ystep = ob.ystep+game.tileH;
} else if (ob.ystep>ob.y) {
var ytile = Math.floor(ob.ystep/game.tileH);
var yold = ytile+game.halfvisy+1;
var ynew = ytile-game.halfvisy-1;
for (var i = ob.xtile-game.halfvisx-1; i<=ob.xtile+game.halfvisx+1; ++i) {
changeTile(i, yold, i, ynew, _root["myMap"+game.currentMap]);
}
ob.ystep = ob.ystep-game.tileH;
}
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();
-
Ciao marco,
se ferma il movimento y del sfondo (potrebbe per esempio togliere tutta la parte da// going up e // if going down), quel giocco diventa proprio inutile. Il movimento e basato sul movimento del sfondo e non sul char che rimane sempre a un punto fisso. Basta che il char sia davanti un blocco e non puo piu contonarlo. Sarebbe da modificare il tutto per renderlo usabile.:bho: