|
SWiSH Forum Forum użytkowników programów SWiSH |
|
SWiSH Max - Logika labiryntu
vanioch - Sro 21 Gru, 2011 21:30 Temat postu: Logika labiryntu Witam, pytanie bardzo ogólne, mam kilkanaście shapów które tworzą ściany do tego kulkę której zadaniem jest odbijanie się od ścian jak w pacmanie z tym że dla jednej ściany to nie jest problem ale jak mam 50 obiektów to jak mam to zrobić?
w Scenie zmienna globalna
var kierunek=2;
kod kulki:
Kod: |
onSelfEvent (enterFrame) {
if (_root.ball.hitTest(_root.sciana)){
kierunek=kierunek-2;
_root.ball._x=_root.ball._x+kierunek;}
}
|
i jak widać jest to tylko odbijanie wg osi x w prawo. Dochodzi logika jeszcze kierunku góra dół w zależności od tego gdzie jest miejsce do chodzenia jak w pacmanie, ktoś pomoże albo ktoś może robił coś już w tym stylu?
Januszr - Sro 21 Gru, 2011 22:02
Już pisałem, że zrobiłem labirynt... Tylko, że ja porobiłem cegiełki, wpisałem skrypty a potem skopiowałem milion razy
espe1 - Sro 21 Gru, 2011 22:25
pełny kod do pacmana:
Kod: |
Scene 1
Actions Layer/Frame 1
------------------------
//Button code - jumps to scene 2
stop();
intro_btn.onRelease = function() {
nextScene();
}
------------------------
Scene 2:
initialize Layer/Frame 1
------------------------
//Initializing variables
_global.Tilewidth = 30; //Tile width
_global.Tileheight = 30; //Tile height
var mazeDepth:Number = 1000; //Starting instance level
var score:Number = 0; //Starting score
var winScore:Number = 0; //End score (defined in map Array)
var obstacle:Number = 2; //Variable to indicate which elements are solid
var speed:Number = Tilewidth/3; //Variable to indicate player speed
------------------------
Scene 2:
sounds Layer/Frame 1
------------------------
//The sounds layer creates two new sound objects,
//"grab" and "done", and attaches linked sounds
//from the library.
var grab_sound:Sound = new Sound();
var done_sound:Sound = new Sound();
grab_sound.attachSound("grab");
done_sound.attachSound("done");
------------------------
Scene 2:
functions Layer/Frame 1
------------------------
/* Resource Functions */
//The addSprite function attaches instances of the sprite name to the cell container.
function addSprite(sprite, mc_name, x, y) {
cell.attachMovie(sprite, mc_name, mazeDepth--);
cell[mc_name]._x = x*Tilewidth;
cell[mc_name]._y = y*Tileheight;
}
//The to_tile function recalculates the x and y values passed to it based on their position relative to a tile on the stage.
function to_tile(x, y) {
var tileX = Math.floor(x/Tilewidth);
var tileY = Math.floor(y/Tileheight);
return {x:tileX, y:tileY};
}
//The get_tile function, conversely, gets the identity of the tile located at the x and y positions.
//(returning a value of either 0, 1 or 2)
function get_tile(x, y) {
var tileX = Math.floor(x/Tilewidth);
var tileY = Math.floor(y/Tileheight);
return (map[tileY][tileX]);
}
function initTiles() {
//Define the height and width of the maze by getting the length of each array element.
var height:Number = map.length;
var width:Number = map[0].length;
//This set of nested for loops builds the maze, looping through the array
//and placing tiles on the stage, then
//assigning them values based on their position and jumping to the proper display frame
//(either a balloon or a tree).
for (y=0; y<height; y++) {
for (x=0; x<width; x++) {
var mc_name = "t"+x+"_"+y;
addSprite("tile", mc_name, x, y);
cell[mc_name].p = to_tile(cell[mc_name]._x, cell[mc_name]._y);
cell[mc_name].gotoAndStop(map[y][x]+1);
if (map[y][x] == 0) { winScore++; }
if (map[y][x] == 1) { charLoc = [y, x]; }
}
}
}
function initPuffin() {
//Place an instance of the playerMC on stage (given the instance name 'player')
addSprite("playerMC", "player", charLoc[0], charLoc[1]);
//Create the 'characterName' variable as a shortcut, to reference the much longer nested name.
_global.characterName = cell.player;
//Character stands still until the player presses a key.
characterName.puffin_mc.gotoAndStop("still");
inMotion = false;
//Define motion and collision detection variables.
//The bounds variable is referencing a built-in Movieclip method to obtain 4 variables related to the character - xMax, yMax, xMin, and yMin.
//The speed of the character is relative to the size of the tiles
bounds = characterName.getBounds(characterName);
}
//The set_coords function is used for collision checking against a tile.
function set_coords() {
var a = to_tile(characterName._x, characterName._y);
_global.playerc = {x:a.x, y:a.y};
}
//The move function is called whenever the player presses (or holds) a key.
function mc_move(x, y, bounds) {
var top = Math.floor(characterName._y+y);
var bot = Math.floor(characterName._y+y+bounds.yMax/2);
var lef = Math.floor(characterName._x+x);
var rig = Math.floor(characterName._x+x+bounds.xMax/2);
switch (obstacle) {
case (get_tile(lef, top)) :
case (get_tile(lef, bot)) :
case (get_tile(rig, top)) :
case (get_tile(rig, bot)) :
break;
default :
characterName._x += x;
characterName._y += y;
break;
}
}
function collision(x, y) {
var mc_name = "t"+x+"_"+y;
if (cell[mc_name].p.x == x && cell[mc_name].p.y == y) {
grab_sound.start();
score++;
//If the player has grabbed all of the balloons, then they win and the victory sound "done" plays.
if (score>=winScore) {
score = "YAY";
//gotoAndStop("done");
done_sound.start();
}
//Removing the movie clip will leave an empty space on the stage where the balloon once was.
removeMovieClip(cell[mc_name]);
}
}
------------------------
Scene 2:
actions Layer/Frame 1
------------------------
//Create a container movie clip named 'cell' and make it the movie's root
createEmptyMovieClip("cell", 1);
cell._x = Tilewidth;
cell._y = Tileheight;
cell._lockroot;
//The next variable is an array filled with 3 possible values where 0 is a balloon, 1 is the player character (Puffin), and 2 is a solid obstacle (a tree).
//This makes the maze easily managable.
/***********************************************************/
_global.map = [[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],
[2,1,0,0,0,2,0,2,0,0,0,0,0,0,2],
[2,0,2,2,0,2,0,2,0,2,2,2,2,0,2],
[2,0,2,0,0,0,0,0,0,0,0,0,2,0,2],
[2,0,0,0,0,2,0,2,0,2,0,0,0,0,2],
[2,0,2,2,0,2,2,2,2,2,0,2,2,0,2],
[2,0,0,2,0,0,0,2,0,0,0,2,0,0,2],
[2,2,2,2,0,2,2,0,2,2,0,2,2,2,2],
[2,0,0,2,0,0,0,0,0,0,0,2,0,0,2],
[2,0,2,2,0,2,2,2,2,2,0,2,0,0,2],
[2,0,0,0,0,2,0,2,0,2,0,0,0,0,2],
[2,0,2,0,0,0,0,0,0,0,0,0,2,0,2],
[2,0,2,0,2,2,0,2,0,2,2,2,0,0,2],
[2,0,0,0,0,2,0,2,0,2,2,0,0,0,2],
[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]];
/***********************************************************/
//Place the tiles
initTiles();
//Define the player character
initPuffin();
------------------------
Scene 2:
listener Layer/Frame 1
------------------------
//Create a listener for keypresses.
onKeyDown = function () {
// Use Switch to check for four possible valid key presses
switch (Key.getCode()) {
case Key.UP :
characterName.puffin_mc._rotation = 0;
if (inMotion == false) { characterName.puffin_mc.gotoAndPlay("rtTurn"); }
inMotion = true;
mc_move(0, -speed, bounds);
collision(playerc.x, playerc.y);
break;
case Key.RIGHT :
characterName.puffin_mc._rotation = 90;
if (inMotion == false) { characterName.puffin_mc.gotoAndPlay("rtTurn"); }
inMotion = true;
mc_move(speed, 0, bounds);
collision(playerc.x, playerc.y);
break;
case Key.DOWN :
characterName.puffin_mc._rotation = 180;
if (inMotion == false) { characterName.puffin_mc.gotoAndPlay("ltTurn"); }
inMotion = true;
mc_move(0, speed, bounds);
collision(playerc.x, playerc.y);
break;
case Key.LEFT :
characterName.puffin_mc._rotation = 270;
if (inMotion == false) { characterName.puffin_mc.gotoAndPlay("ltTurn"); }
inMotion = true;
mc_move(-speed, 0, bounds);
collision(playerc.x, playerc.y);
break;
default :
trace("no case tested true");
}
// After the proper motion has been proccessed, reset the coords position so the collision detection can take place on the next keypress.
set_coords();
};
//onKeyUp tell the character to stop it's walking animation
onKeyUp = function () {
characterName.puffin_mc.gotoAndStop("still");
inMotion = false;
};
Key.addListener(this);
++++++++++++ END ++++++++++++++++
|
pożyczone z:
http://www.peachpit.com/a...e.aspx?p=169458
Januszr - Czw 22 Gru, 2011 06:33
http://forums.swishzone.c...showtopic=72872
vanioch - Pon 26 Gru, 2011 22:10
Witam ponownie, mam dwie przeszkody w postaci kwadracików. Celem jest to aby piłka łaziła losowo po labiryncie jak duchy w pacmanie, i jest kłopot bo jak mam wybierać kierunek? W załączonym swi jest ten problem że piłka utknęła miedzy dwoma przeszkodami
espe1 - Wto 27 Gru, 2011 19:31
Chcesz napisać grę pacman w 3 linijkach kodu???
Ściągnij sobie gotowca OC_pacman swi ze strony http://www.swishresource.com/
po rozpakowaniu zajrzyj do pliku z nazwą basic (wersja uproszczona)
|
|