Copy link to clipboard
Copied
I'm using Adobe Animate CC for this job
I added these buttons on the stage inside my vCam but I can not make it work. Can someone help me to make the buttons on the stage work?
attack
jump
right
left
Here is the file:
https://www.dropbox.com/s/7d9o52eajqy1icj/testPlayer1.fla?dl=0
Thanks in advance,
Emiliano
OK.
I just updated the link with a new sample that uses two buttons to move the character to the left and to the right.
AS3 code:
...import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
var player:MovieClip = cam.player;
var map:MovieClip = cam.map;
var buttonForce:uint = 10;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
function mouseDownHandler(e:MouseEvent):void
{
if (e.target == leftButton)
leftPressed = true;
else if (e.target == ri
Copy link to clipboard
Copied
What code do you have for these controls now (keyboard controls)? (some folks are okay with opening files people link - I'm not one of them)
Copy link to clipboard
Copied
Hello this is the cone for the player
import flash.events.Event;
stop();
vCam.NumOvejas.text=0;
stage.addEventListener(KeyboardEvent.KEY_DOWN,presionoTecla);
stage.addEventListener(KeyboardEvent.KEY_UP,levantoTecla);
mcPlayer1.addEventListener(Event.ENTER_FRAME, AplicarMovimiento);
var TeclaSpacePresionada:Boolean = false;
var keysDown:Object = new Object();
var arriba:Number = 38;
var abajo:Number = 40;
var izquierda:Number = 37;
var derecha:Number = 39;
var space:Number = 32;
var control:Number = 17;
function presionoTecla(evt:KeyboardEvent):void
{
keysDown[evt.keyCode] = true;
}
function levantoTecla(evt:KeyboardEvent):void
{
delete keysDown[evt.keyCode];
}
function teclaPresionada(keyCode:uint):Boolean
{
return Boolean(keyCode in keysDown);
}
var VelocidadY:Number = 10;
var Velocidad:Number =5;
var Limite:Number = 20;
var Salto:Number = -12;
var DobleSalto:Boolean = false;
var ODobleSalto:Boolean = false;
var AnimaSaltoDoble:Boolean = false;
function AplicarMovimiento(e:Event):void{
var Mcj = mcPlayer1;
var Elnivel = mcSuelo;
if(Elnivel.hitTestPoint(Mcj.x,Mcj.y+20,true)){
while(Elnivel.hitTestPoint(Mcj.x, -2 +Mcj.y +20,true)){
Mcj.y--;
}
VelocidadY =0;
DobleSalto=true;
if(teclaPresionada(arriba)){
VelocidadY = Salto;
Mcj.y+=VelocidadY;
}
}
else if(Elnivel.hitTestPoint(Mcj.x, Mcj.y -20,true)){
Mcj.y+=VelocidadY;
while(Elnivel.hitTestPoint(Mcj.x, Mcj.y -20,true)){
Mcj.y++;
}
}
else{
Mcj.y += VelocidadY;
if(VelocidadY<=Limite){
VelocidadY++;
}
if(ODobleSalto){
if(DobleSalto && VelocidadY>=0 && teclaPresionada(arriba)){
AnimaSaltoDoble = true;
DobleSalto = false;
VelocidadY = Salto;
}
}
}
while(Elnivel.hitTestPoint(Mcj.x-10, Mcj.y,true)){
Mcj.x++;
}
while(Elnivel.hitTestPoint(Mcj.x+10, Mcj.y,true)){
Mcj.x--;
}
}
var animationState:String = "Normal";
addEventListener(Event.ENTER_FRAME, ChecarAnimaciones);
function ChecarAnimaciones(e:Event):void
{
if (mcPlayer1.currentLabel != animationState)
{
mcPlayer1.gotoAndPlay(animationState);
}
if (teclaPresionada(izquierda))
{
animationState = "Caminando";
mcPlayer1.scaleX = -1;
TeclaSpacePresionada = false;
mcPlayer1.x -= Velocidad;
}
else if (teclaPresionada(derecha))
{
animationState = "Caminando";
mcPlayer1.scaleX = 1;
TeclaSpacePresionada = false;
mcPlayer1.x += Velocidad;
}
else if (teclaPresionada(space))
{
animationState = "Ataque";
TeclaSpacePresionada = true;
}
else
{
animationState = "Normal";
TeclaSpacePresionada = false;
}
if (teclaPresionada(arriba))
{
TeclaSpacePresionada = false;
animationState = "Salto";
}
if(AnimaSaltoDoble){
animationState = "SaltoDobleAnimacion";
}
}
this.addEventListener( Event.ENTER_FRAME, tocar1);
function tocar1( e:Event ):void
{
if (mcPlayer1.hitTestObject(vCam.Acc1))
{
vCam.x -= 5;
Fondo.Mon1.x += 0.8;
Fondo.Mon2.x += 0.4;
}
if (mcPlayer1.hitTestObject(vCam.Acc2))
{
vCam.x += 5;
Fondo.Mon1.x -= 0.8;
Fondo.Mon2.x -= 0.4;
}
if (mcPlayer1.hitTestObject(vCam.Acc3))
{
vCam.y -= 30;
}
if (mcPlayer1.hitTestObject(vCam.Acc4))
{
vCam.y += 20;
}
}
// Monedas
MovieClip(root).vCam.Monedas.text = "0";
this.addEventListener( Event.ENTER_FRAME, tocar1);
function tocar1( e:Event ):void
{
if (mcPlayer1.hitTestObject(vCam.Acc1))
{
vCam.x -= 5;
Fondo.Mon1.x += 0.8;
Fondo.Mon2.x += 0.4;
}
if (mcPlayer1.hitTestObject(vCam.Acc2))
{
vCam.x += 5;
Fondo.Mon1.x -= 0.8;
Fondo.Mon2.x -= 0.4;
}
if (mcPlayer1.hitTestObject(vCam.Acc3))
{
vCam.y -= 30;
}
if (mcPlayer1.hitTestObject(vCam.Acc4))
{
vCam.y += 20;
}
}
// Monedas
MovieClip(root).vCam.Monedas.text = "0";
///////////////////////////////////////////////////////////////this code goes inside the vCam////////////////////////////////
var oldScaleMode:String=stage.scaleMode;
stage.scaleMode="exactFit";
var sW:Number=stage.stageWidth;
var sH:Number=stage.stageHeight;
stage.scaleMode=oldScaleMode;
var bounds_obj:Object=this.getBounds(this);
var camH:Number=bounds_obj.height;
var camW:Number=bounds_obj.width;
var rp:Point=new Point(x,y);
addEventListener(Event.ENTER_FRAME,camControl);
addEventListener(Event.REMOVED_FROM_STAGE,reset);
var bmp:Bitmap;
var myBitmapData:BitmapData=new BitmapData(sW,sH,true,0);
bmp=new Bitmap(myBitmapData);
camControl();
stage.addChild(bmp);
function camControl(...event):void {
parent.visible=true;
rp.x=x;
rp.y=y;
var h:Number=camH * scaleY;
var w:Number=camW * scaleX;
var _scaleY:Number=sH / h;
var _scaleX:Number=sW / w;
x2=w / 2 * _scaleX;
y2=h / 2 * _scaleY;
scaleX2=_scaleX;
scaleY2=_scaleY;
rotation2=- rotation;
myBitmapData.lock();
myBitmapData.fillRect(myBitmapData.rect,0x00);
myBitmapData.unlock();
myBitmapData.draw(stage);
bmp.filters=this.filters;
bmp.transform.colorTransform=this.transform.colorTransform;
parent.visible=false;
}
function reset(e:Event):void {
removeEventListener(Event.ENTER_FRAME,camControl);
removeEventListener(Event.REMOVED_FROM_STAGE,reset);
stage.removeChild(bmp);
myBitmapData.dispose();
bmp=null;
parent.scaleX=1;
parent.scaleY=1;
parent.x=0;
parent.y=0;
parent.rotation=0;
parent.visible=true;
}
function set x2(value:Number):void {
var p:Point=parent.parent.globalToLocal(parent.localToGlobal(rp));
parent.x+= value - p.x;
}
function get x2():Number {
var p:Point=parent.parent.globalToLocal(parent.localToGlobal(rp));
return p.x;
}
function set y2(value:Number):void {
var p:Point=parent.parent.globalToLocal(parent.localToGlobal(rp));
parent.y+= value - p.y;
}
function get y2():Number {
var p:Point=parent.parent.globalToLocal(parent.localToGlobal(rp));
return p.y;
}
function get scaleX2():Number {
return parent.scaleX;
}
function set scaleX2(value:Number):void {
setProperty2("scaleX",value);
}
function get scaleY2():Number {
return parent.scaleY;
}
function set scaleY2(value:Number):void {
setProperty2("scaleY",value);
}
function get rotation2():Number {
return parent.rotation;
}
function set rotation2(value:Number):void {
setProperty2("rotation",value);
}
function setProperty2(prop:String,n:Number):void {
var a:Point=parent.parent.globalToLocal(parent.localToGlobal(rp));
parent[prop]=n;
var b:Point=parent.parent.globalToLocal(parent.localToGlobal(rp));
parent.x-= b.x - a.x;
parent.y-= b.y - a.y;
}
Copy link to clipboard
Copied
Hi.
When you set the parent (main timeline in this case) to be invisible in the last line of the camControl function, your buttons get disabled,
I cannot tell you for sure why this happens.
But anyway: why do you need to make the main timeline invisible?
Regards,
JC
Copy link to clipboard
Copied
Hi,
The vCam layer is shown as an outline but that does not affect the operation of the code
Copy link to clipboard
Copied
What I said is that the last line of this function:
function camControl(...event):void
{
parent.visible=true;
rp.x=x;
rp.y=y;
var h:Number=camH * scaleY;
var w:Number=camW * scaleX;
var _scaleY:Number=sH / h;
var _scaleX:Number=sW / w;
x2=w / 2 * _scaleX;
y2=h / 2 * _scaleY;
scaleX2=_scaleX;
scaleY2=_scaleY;
rotation2=- rotation;
myBitmapData.lock();
myBitmapData.fillRect(myBitmapData.rect,0x00);
myBitmapData.unlock();
myBitmapData.draw(stage);
bmp.filters=this.filters;
bmp.transform.colorTransform=this.transform.colorTransform;
parent.visible=false; // THIS LINE HERE
}
... is affecting your buttons causing them to not work.
Copy link to clipboard
Copied
To be honest I do not know how it should go,
Regards
Copy link to clipboard
Copied
Maybe you can just comment out that line.
And I think this virtual cams are best suited for people doing animation.
For games, you could take a less bloated approach.
Here is a VERY simple example in which the camera follows the player. Please notice that:
- The character moves at a constant speed to not overcomplicate the code.
- Both the camera and the map Movie Clip instances are registered at the top left corner and these points coincide with the origin of the stage.
- The player has the registration point in the middle.
AS3 code:
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
var player:MovieClip = cam.player;
var map:MovieClip = cam.map;
var keyForce:uint = 10;
function keyDownHandler(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.LEFT)
player.x -= keyForce;
if (e.keyCode == Keyboard.RIGHT)
player.x += keyForce;
if (e.keyCode == Keyboard.UP)
player.y -= keyForce;
if (e.keyCode == Keyboard.DOWN)
player.y += keyForce;
limitPlayer();
moveCam();
}
function limitPlayer():void
{
player.x = clamp(player.x, player.width * 0.5, map.width - player.width * 0.5);
player.y = clamp(player.y, player.height * 0.5, map.height - player.height * 0.5);
}
function moveCam():void
{
cam.x = clamp(-player.x + stage.stageWidth * 0.5, -map.width + stage.stageWidth, 0);
cam.y = clamp(-player.y + stage.stageHeight * 0.5, -map.height + stage.stageHeight, 0);
}
function clamp(value:Number, min:Number, max:Number):Number
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
function start():void
{
limitPlayer();
moveCam();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
start();
FLA download:
animate_cc_as3_game_cam.zip - Google Drive
I hope this helps.
Regards,
JC
Copy link to clipboard
Copied
It works perfect, but I still do not know how to make it work with buttons on the stage and without the PC keys
Muito obrigado
Copy link to clipboard
Copied
De nada!
Don't you know how to get the buttons working in your file or in my example?
Copy link to clipboard
Copied
In your example
Copy link to clipboard
Copied
OK.
I just updated the link with a new sample that uses two buttons to move the character to the left and to the right.
AS3 code:
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
var player:MovieClip = cam.player;
var map:MovieClip = cam.map;
var buttonForce:uint = 10;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
function mouseDownHandler(e:MouseEvent):void
{
if (e.target == leftButton)
leftPressed = true;
else if (e.target == rightButton)
rightPressed = true;
}
function mouseUpHandler(e:MouseEvent):void
{
leftPressed = false;
rightPressed = false;
}
function enterFrameHandler(e:Event):void
{
if (leftPressed)
player.x -= buttonForce;
else if (rightPressed)
player.x += buttonForce;
if (leftPressed || rightPressed)
{
limitPlayer();
moveCam();
}
}
function limitPlayer():void
{
player.x = clamp(player.x, player.width * 0.5, map.width - player.width * 0.5);
player.y = clamp(player.y, player.height * 0.5, map.height - player.height * 0.5);
}
function moveCam():void
{
cam.x = clamp(-player.x + stage.stageWidth * 0.5, -map.width + stage.stageWidth, 0);
cam.y = clamp(-player.y + stage.stageHeight * 0.5, -map.height + stage.stageHeight, 0);
}
function clamp(value:Number, min:Number, max:Number):Number
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
function start():void
{
limitPlayer();
moveCam();
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
start();
FLA download:
animate_cc_as3_game_cam.zip - Google Drive
I hope this helps.
Regards,
JC
Copy link to clipboard
Copied
It works perfect! You are a good programmer, thank you very much !!!
Copy link to clipboard
Copied
You're welcome!!!
Have an awesome weekend!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now