Во второй части движка, я добавляю ограничительные стеныЭто делается так же, как в опубликованной 1 части, просто добавьте стены и столкновения. Одно замечание… точка привязки героя сейчас находится в центре, а в предыдущем примере она находилась в левом верхнем углу.
ACTIONSCRIPT: tile_size = 20 ;
ground_acceleration = 1 ;
ground_friction = 0 .8 ;
ice_acceleration = 0 .15 ;
ice_friction = 0 .95 ;
treadmill_speed = 2 ;
max_speed = 3 ;
xspeed = 0 ;
level = new Array ( ) ;
level [ 0 ] = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
level [ 1 ] = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
level [ 2 ] = [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ;
level [ 3 ] = [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ;
level [ 4 ] = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 1 , 3 , 3 , 3 , 3 , 1 , 1 , 4 , 4 , 4 , 4 , 1 , 1 ] ;
player = [ 1 , 3 ] ;
function create_level( l) {
_root .createEmptyMovieClip ( "level_container" , 1 ) ;
level_height = l.length ;
level_width = l[ 0 ] .length ;
for ( y=0 ; y<level_height; y++) {
for ( x=0 ; x<level_width; x++) {
if ( l[ y] [ x] != 0 ) {
t = level_container.attachMovie ( "tile" , "t" +y+"_" +x, _root .level_container .getNextHighestDepth ( ) , { _x :x*tile_size, _y :y*tile_size} ) ;
t.gotoAndStop ( l[ y] [ x] ) ;
}
}
}
x_pos = player[ 0 ] *tile_size+tile_size/2 ;
y_pos = player[ 1 ] *tile_size+tile_size/2 ;
level_container.attachMovie ( "hero" , "hero" , _root .level_container .getNextHighestDepth ( ) , { _x :x_pos, _y :y_pos} ) ;
}
create_level( level ) ;
_root .onEnterFrame = function ( ) {
ground_under_feet( ) ;
walking = false ;
if ( Key .isDown ( Key .LEFT ) ) {
xspeed -= speed;
walking = true ;
}
if ( Key .isDown ( Key .RIGHT ) ) {
xspeed += speed;
walking = true ;
}
if ( !walking) {
xspeed *= friction;
if ( Math .abs ( xspeed) <0 .5 ) {
xspeed = 0 ;
}
}
if ( xspeed>max_speed) {
xspeed = max_speed;
}
if ( xspeed<-max_speed) {
xspeed = -max_speed;
}
xspeed += bonus_speed;
x_pos += xspeed;
check_collisions( ) ;
level_container.hero ._x = x_pos;
xspeed -= bonus_speed;
} ;
function ground_under_feet( ) {
bonus_speed = 0 ;
y_feet = Math .floor ( y_pos/tile_size) +1 ;
x_feet = Math .floor ( ( x_pos+tile_size/2 ) /tile_size) ;
switch ( level [ y_feet] [ x_feet] ) {
case 1 :
over = "ground" ;
speed = ground_acceleration;
friction = ground_friction;
break ;
case 2 :
over = "ice" ;
speed = ice_acceleration;
friction = ice_friction;
break ;
case 3 :
over = "treadmill" ;
speed = ground_acceleration;
friction = ground_friction;
bonus_speed = -treadmill_speed;
break ;
case 4 :
over = "treadmill" ;
speed = ground_acceleration;
friction = ground_friction;
bonus_speed = treadmill_speed;
break ;
}
}
function check_collisions( ) {
get_edges( ) ;
// collision to the left
if ( xspeed<0 ) {
if ( ( level [ top] [ left ] != 0 ) or ( level [ bottom] [ left ] != 0 ) ) {
x_pos = ( left +1 ) *tile_size+5 ;
xspeed = 0 ;
}
}
// collision to the right
if ( xspeed>0 ) {
if ( ( level [ top] [ right ] != 0 ) or ( level [ bottom] [ right ] != 0 ) ) {
x_pos = ( right ) *tile_size-5 ;
xspeed = 0 ;
}
}
}
function get_edges( ) {
// right edge
right = Math .floor ( ( x_pos+6 -1 ) /tile_size) ;
// left edge
left = Math .floor ( ( x_pos-6 ) /tile_size) ;
// bottom edge
bottom = Math .floor ( ( y_pos+9 -1 ) /tile_size) ;
// top edge
top = Math .floor ( ( y_pos+9 ) /tile_size) ;
}
скачать исходники