adobe animate virtual camera can't pin camera - actionscript-3

I'm trying to utilize the pinCameraToObject in virtual camera in adobe animate cc 2019.
Documentation: https://helpx.adobe.com/animate/using/working-with-camera-in-animate.html#Lockingalayerwithcamera
the problem is it doesn't do as it says it should, it doesn't pin, infact it does nothing. Other features such as setPosition work, but not pin. I have a movieclip mc on the stage that I want to pin to. I've checked and made sure movieclip is loaded on stage and all childrens to be declared on run has been enabled. When I move the mc movieclip the virtual camera does not follow.
My code:
package {
import flash.display.MovieClip;
import fl.VirtualCamera;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
public class Cam extends MovieClip {
public var cameraObj = fl.VirtualCamera.getCameraAsMovieClip(root);
public function Cam() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
cameraObj.pinCameraToObject(getChildByName("mc"), 200,50);
}
public function reportKeyDown(event: KeyboardEvent): void {
if (event.keyCode == Keyboard.W) {
mc.x = mc.x + 25;
}
}
}
}

Related

Main class was not the first class which was called in ActionScript 3.0

I have a weird problem in a game which I want to create. At first I have created a project without external classes.
On the root I have three Characters and one Level. Also there is a script for the key listeners and I have eventListeners to register the level, levelElements, coins and the characters. Then I have a CharacterControl MovieClip in the library. This MovieClip contains the character behaviour. As example walk, jump, idle, gravity if not colliding to the ground. There are also different events and eventListeners.
The scripts are on the timeline. If I call in both timelines a trace-function, the root was called before the CharacterController.
After that in my next exercise I created a document class Main. Now there are all root scripts. And for the CharacterController I also copied the timeline code and put it into an external class.
Now my problem is that the CharacterController class is called before the main class gets called. This leads to the problem that the eventListener and events can't get called in right order. There are happening a few errors. No Coin and no Character collides on the ground or a plattform. Everything is falling down.
How can I achieve that the Main gets called at first? Should I remove the characters and create them by script?
EDIT:
Ok, I give a short example which shows the basic problem without the complex code of my game.
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
trace("main was called");
}
}
}
package {
import flash.display.MovieClip;
public class My_Circle extends MovieClip {
public function My_Circle() {
// constructor code
trace("circle was called");
}
}
}
Here are some pictures of the configuration and structure of my project:
I need Main called as first. I think it's a basic problem in as3.
You'd make the class file of your stage Main.as in the properties pane.
Edit: Interesting. Just replicated this. I believe then that flash/air constructs elements so they're ready to be put on the stage instead of constructing the stage first and elements after. You should put the code you want to execute for your circle in some sort of init function and execute it in.
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
super();
trace("Hello");
(circle_mc as Circle).init();
}
}
}
Circle:
package
{
import flash.display.MovieClip;
public class Circle extends MovieClip
{
public function Circle()
{
super();
}
public function init():void
{
trace("World");
}
}
}
ok, I figured out a simple solution how you can make it by code. At first I think it's a better solution to create the object (circle, character, whatever) by code.
The timeline code:
import flash.events.Event;
Main.setStageRef(this.stage); //Super-important
stop();
The Main class code:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
import flash.events.Event;
public class Main extends MovieClip {
public static var stageRef : Object = null;
var movieClipExample : My_Circle;
public function Main() {
this.addEventListener(Event.ENTER_FRAME,this.afterMainTimeLine);
stage.addEventListener("myEvent", myEventFunction);
}
public function afterMainTimeLine(e:Event) {
trace("Hello");
movieClipExample = new My_Circle;
movieClipExample.init();
stageRef.addChild(movieClipExample);
removeEventListener(Event.ENTER_FRAME, this.afterMainTimeLine);
this.addEventListener(Event.ENTER_FRAME,this.updateFunction);
}
public function updateFunction(e:Event){
movieClipExample.moveFunction();
}
public function myEventFunction(_event: Event) {
trace("myEvent was triggered");
}
//Getter/setter for stage
public static function setStageRef(_stage : Object) : void
{
stageRef = _stage;
}
public static function getStageRef() : Object
{
return stageRef;
}
}
}
The Object code (as example My_Circle):
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.DisplayObject;
public class My_Circle extends MovieClip {
public function My_Circle()
{
stageRef = Main.getStageRef();
trace("World");
this.x = x;
this.y = y;
var evt = new Event("myEvent", true);
stageRef.dispatchEvent(evt);
}
public function init():void
{
trace("Created Circle");
this.x = 300;
this.y = 100;
}
function moveFunction(){
this.y += 1;
}
}
}
The output is following:
Hello
World
myEvent was triggered
Created Circle
Maybe this could be helpful for others.
Just one thing. For different objects from the same class it's better to use an array. The position (maybe) should be random.

AS3 debugger stops responding while trying to load image into sprite using Loader

I'm trying to create a simple Menu in AS3. There is a sprite called startButton, which when pressed will call the function startGame, and that's it! But, not so easy. I'm using flashdevelop IDE so I'm trying to call a loader to get a .png image file for the spite startButton. But, it doesn't work. There are no error messages, the debugger just does not respond. Any help? Here is the code for both files
Main code:
package {
//Other Files
import Menu;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Main extends Sprite {
//Game values
public static var gameWidth:int = 750;
public static var gameHeight:int = 750;
public function Main() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
addChild(Menu.startButton);
Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
stage.addEventListener(Event.ENTER_FRAME, update);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
//Function starts game
public function startGame(evt:MouseEvent):void {
removeChild(Menu.startButton);
}
//Updates every 60 seconds
public function update():void {
trace("Updated");
}
}
}
And Menu Image code:
package {
//Other files
import Main;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class Menu extends Sprite {
public static function imageLoaded():void {
startButton.addChild(loader);
//initizlize values for startButton Bitmap
startButton.x = (Main.gameWidth / 2) - (startButton.width / 2);
startButton.y = (Main.gameHeight / 2) - (startButton.height / 2);
}
//create startButton Bitmap
public static var startButton:Sprite = new Sprite();
public static var loader:Loader = new Loader();
loader.load(new URLRequest("lib/menustartbutton.png"));
loader.addEventListener(Event.COMPLETE, imageLoaded);
}
}
By the way, I wait for the loader to successfully load the image before working with it, just in case the image takes more time and it draws errors.
The problem is that you misuse static. all static methods/properties are initialized before the classes themselves. As a result static can receive values but they cannot run any code. Running code has to happen after all classes are ready to go which is not the case when static is initialized. In your case startButton and loader are created correctly but the next line never runs 'loader.load'.
Don't misuse static, you are obviously trying to use static to make you code writing and life easier but at the end because you are misusing it you will always end up with more problems.

Add MovieClip with Class to Stage

I'm new to AS3 and seem to be overlooking something related to classes and/or targeting. I have a MovieClip in my Library that is linked to a class called 'cloud' (yes cloud is just a picture of a rain cloud). I want to add cloud to the stage. I understand there are two ways to do this:
Drag it directly across from the Library to the Stage.
Use AS3 to addChild to the Stage.
The class works when I publish my swf using the "drag it directly across from the Library to the Stage" approach. But it doesn't work when I try using the AS3 approach.
The class is as follows:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class cloud extends MovieClip {
public function cloud() {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyMapping);
}
private function keyMapping(event:KeyboardEvent):void {
if (event.charCode == 13) {
MovieClip(parent).testme.text = "hello world!";
}
}
}
}
The AS3 I have been using on Frame 1 of my main timeline:
stop();
var cloud_mc:cloud = new cloud();
addChild(cloud_mc);
==================================================================================
SUGGESTED FIX
Class:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class cloud extends MovieClip {
public function cloud() {
}
public function tralala() {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyMapping);
}
private function keyMapping(event:KeyboardEvent):void {
if (event.charCode == 13) {
MovieClip(parent).testme.text = "hello world!";
}
}
}
}
AS3:
stop();
var cloud_mc:cloud = new cloud();
addChild(cloud_mc);
cloud_mc.tralala();
Use the linkage name of the library object, not the class that it extends.
Your library object extends the cloud class which extends movie clip. If you instanciate the cloud class you miss the library object.
The process is described in detail here:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b8ea63-7fee.html

how to add a movieclip to second frame of main timeline through document class?

i simply have one layer which has two frames
frame 1 : menu with only one button
frame 2 : blank but with document class want to put Movie Clip named circle
I want to put it that way because i will program the symbol to draw ....(till now wrote nothing)
In document class till now i have written
package
{
//list of our imports these are classes we need in order to
//run our application.
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class engine extends MovieClip
{
// moved ourShip to a class variable.
private var ourShip:circle = new circle()
//our constructor function. This runs when an object of
//the class is created
public function engine()
{
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
addChild(ourShip)
}
}
}
First frame button as file :
package
{
//imports
import flash.events.MouseEvent;
import flash.display.SimpleButton;
import flash.display.MovieClip;
//-------
public class start extends SimpleButton
{
public function start()
{
addEventListener(MouseEvent.CLICK, onTopClick);
addEventListener(MouseEvent.MOUSE_OVER, onBottomOver);
}
function onTopClick(e:MouseEvent):void
{
MovieClip(root).gotoAndStop(2)
}
function onBottomOver(e:MouseEvent):void
{
}
}
}
By the way i only know addchild (which i wrote in as here,right now it would display circle in both frame 1 and frame 2 but i want it in only frame 2)
Because of how the Flash timeline works, you can only add something to a frame when you are there. So you can do,
goToAndStop(3);
addChild(myClip);
But then if you leave frame 3 it is gone forever.
You can
Add the code to the individual frame of the timeline, or
You can create a simple Event listener like below
Adding Event Listener:
addEventListener(Event.ENTER_FRAME, function() {
if (this.currentFrame == 3) {
addChild(myClip)
}
})

calling movieclip of a class from another class, error#1009

i got error: method of null object reference. I'm so confused and don't know what is the real cause.
So i got a player movieClip on the stage which has an instance of "player_mc", which will be pass thru my Document class and into the Player class.
Player.as
import flash.display.*;
import flash.events.*;
public class Player extends MovieClip
{
public var myPlayer:MovieClip;
public function Player(player:MovieClip)
{
myPlayer = player;
addEventListener(Event.ENTER_FRAME,on_enter);
}
Document.as
import flash.display.*;
import Components.Player.Player;
public class Game_Main extends MovieClip
{
public var player:Player;
public function Game_Main()
{
player = new Player(player_mc);
}
}
now here i think is where the problem comes from. I have a green_enemy movieclip on the stage which has base class Enemy.
Enemy.as
import flash.display.MovieClip;
import Components.Player.Player;
import flash.events.Event;
public class Enemy extends MovieClip
{
var theplayer:Player;
public function Enemy()
{
this.addEventListener(Event.ENTER_FRAME,on_enter);
}
public function on_enter(e:Event):void
{
if (this.hitTestObject(theplayer.myPlayer)) //calls player_mc from Player class
{
trace("hi");
}
}
}
what I like to do on Enemy function is when Enemy collides with "player_mc" (which is on the stage) it will do something. Maybe my code is wrong.
Any help/tips will be appreciated. Thanks!
In your Enemy.as I see
var theplayer:Player;
not initialized. It is private, so you can not define it from outside. That means that exception comes from here
this.hitTestObject(theplayer.myPlayer)
You are trying to call myPlayer from null.
Try to define this variable while constructing Enemy class.
To prevent null exception you can check if theplayer is null
public function on_enter(e:Event):void
{
if (theplayer && this.hitTestObject(theplayer.myPlayer))
{
trace("hi");
}
}
To expand on what ZuyEL said, your variable isn't globally defined in your enemy class since you're just adding a var inside your enemy function. Instead, do the following
public var thePlayer:Player;
write the above after your Enemy class, before the Enemy function. Now it is available to the whole class and will no longer be null when you call it later.