0

Criando um game básico de plataforma em AS3

Posted by Eduardo Brito on 16:54
Sumário: Olá, nesse tutorial ensinarei um pouco de AS3, e vem uma série de games em AS3, um jogo de plataforma básico com movimentos e pulo só para praticar.

1.Bem vamos começar criando um personagem, eu criei uma bola (êta criatividade!) (obs: Coloque o frame rate em 30, e crie seu personagem no fim do documento pois lá será o solo)

Image and video hosting by TinyPic

2.transformem ela em movie clip e coloque seu instance name de "mcMain" (sem aspas)

Image and video hosting by TinyPic

3.Agora copie o código e cole na 1° frame (é claro q no AS3 não podemos colocar no movieclip) esses códigos vão fazer nosso personagem se mover e pular:

//These variables will note which keys are down
//We don't need the up or down key just yet
//but we will later
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
//the main character's speed
var mainSpeed:Number = 7;
//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:int = 15;
//the current speed of the jump;
var jumpSpeed:Number = 0;

//adding a listener to mcMain which will make it move
//based on the key strokes that are down
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
//if certain keys are down then move the character
if(leftKeyDown){
mcMain.x -= mainSpeed;
}
if(rightKeyDown){
mcMain.x += mainSpeed;
}
if(upKeyDown || mainJumping){
mainJump();
}
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void{
//making the booleans true based on the keycode
//WASD Keys or arrow keys
if(event.keyCode == 37 || event.keyCode == 65){
leftKeyDown = true;
}
if(event.keyCode == 38 || event.keyCode == 87){
upKeyDown = true;
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
}
if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = true;
}
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
//making the booleans false based on the keycode
if(event.keyCode == 37 || event.keyCode == 65){
leftKeyDown = false;
}
if(event.keyCode == 38 || event.keyCode == 87){
upKeyDown = false;
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = false;
}
if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = false;
}
}

//jumping function
function mainJump():void{
//if main isn't already jumping
if(!mainJumping){
//then start jumping
mainJumping = true;
jumpSpeed = jumpSpeedLimit*-1;
mcMain.y += jumpSpeed;
} else {
//then continue jumping if already in the air
//crazy math that I won't explain
if(jumpSpeed < 0){
jumpSpeed *= 1 - jumpSpeedLimit/75;
if(jumpSpeed > -jumpSpeedLimit/5){
jumpSpeed *= -1;
}
}
if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
jumpSpeed *= 1 + jumpSpeedLimit/50;
}
mcMain.y += jumpSpeed;
//if main hits the floor, then stop jumping
//of course, we'll change this once we create the level
if(mcMain.y >= stage.stageHeight - mcMain.height){
mainJumping = false;
mcMain.y = stage.stageHeight - mcMain.height;
}
}
}


Explicação:

//jumping function
function mainJump():void{
//if main isn't already jumping
if(!mainJumping){
//then start jumping
mainJumping = true;
jumpSpeed = jumpSpeedLimit*-1;
mcMain.y += jumpSpeed;
} else {
//then continue jumping if already in the air
//crazy math that I won't explain
if(jumpSpeed < 0){
jumpSpeed *= 1 - jumpSpeedLimit/75;
if(jumpSpeed > -jumpSpeedLimit/5){
jumpSpeed *= -1;
}
}
if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
jumpSpeed *= 1 + jumpSpeedLimit/50;
}
mcMain.y += jumpSpeed;
//if main hits the floor, then stop jumping
//of course, we'll change this once we create the level
if(mcMain.y >= stage.stageHeight - mcMain.height){
mainJumping = false;
mcMain.y = stage.stageHeight - mcMain.height;
}
}
}


Este código faz o personagem pular simplismente(jump), e o começo do código faz os movimentos do personagem (key).

Até a próxima!
fonte: www.mrsunstudios.com
(continuaremos com AS3)
Opniões e dúvias, comentem.

0

Criando um jogo de Mouse

Posted by Eduardo Brito on 11:44
Súmario: veja o resultado dese jogo AQUI

1.Crie duas layersde o nome de uma AS e da outra "MC's".

Image and video hosting by TinyPic

2.Agora na layer MC's crie uma bola grande com outra na ponta, e outra bolinha pequena que será o mouse.

Image and video hosting by TinyPic

3.Converta a bola grande em MovieClip e dê o nome que quiser, no coloque exatamente isto: "mochithing" (sem aspas), agora Converta a bola pequena em MC e dê o seu de: "mousep" (sem aspas)

Image and video hosting by TinyPic



4.Agora na frame AS na 1° frame abra a janela de ações (F9) e digite a seguinte ação:





Mouse.hide();
delay=10;
_root.attachMovie("mousep","mousep",_root.getNextHighestDepth());
_root.attachMovie("mochithing","mochithing",_root.getNextHighestDepth());
mousep.onEnterFrame = function() {
this._x = _root._xmouse;
this._y = _root._ymouse;
};
mochithing.onEnterFrame = function() {
dist_x = mousep._x-this._x;
dist_y = mousep._y-this._y;
distance = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
angle = Math.atan2(dist_y, dist_x);
speed = distance/delay;
xspeed = speed*Math.cos(angle);
yspeed = speed*Math.sin(angle);
this._x += xspeed;
this._y += yspeed;
this._rotation = angle*180/Math.PI
};



Obs:para seu jogo ficar mais rápido coloque 30 no frame rate

Fim! agora é só testar o jogo e se divertir!
até a próxima!
fonte: http://www.emanueleferonato.com

Copyright © 2009 .:EBL Design:. All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive. Distribuído por Templates