I'm developing a mobile project with Flash Builder, and I'm having some issues with the Tween Class... It works just fine for some time, but then, all of the sudden, it just stops working.
The game emulates an "under the sea" scene, so, one of the things I have is a "bubbly" background, and it works just fine for some time, and then, all of the sudden, all the tweens stop at the same time (including some tweens that are in other classes). Any thoughts on why this could happen? I'm adding the code of the background bubbles to see if you get why this could be behaving so oddly.
Thanks in advance!
public class BurbujasBack extends Sprite
{
public var timer:Timer = new Timer(200,0);
public var burbujasMaximas:int = 25;
public static var burbujasActuales:int = 0;
public function BurbujasBack()
{
x=Diversion_bajo_el_agua.ancho_st/2;
y=Diversion_bajo_el_agua.alto_st;
timer.addEventListener(TimerEvent.TIMER,_nuevaBubble);
timer.start();
}
private function _nuevaBubble(e:TimerEvent):void {
var add:Boolean = (Math.random() > .5) ? true : false;
if (add==true) {
if(burbujasActuales < burbujasMaximas) {
trace("agrega una burbuja");
var burbuja:BurbujaChica = new BurbujaChica();
addChild(burbuja);
burbujasActuales++;
}
}
}
}
And
public class BurbujaChica extends Sprite
{
public var velocidad:Number = Math.random();
public var escala:Number = Math.random()*.5+.5;
public var tiempoMaximo:int = 3;
public var posX:Number = Math.random()*Diversion_bajo_el_agua.ancho_st;
public function BurbujaChica()
{
x = posX
var burbuja:Burbuja_ = new Burbuja_();
burbuja.scaleX = burbuja.scaleY = escala;
var desde:Number = burbuja.height/2;
var hasta:Number = -Diversion_bajo_el_agua.alto_st-burbuja.height/2;
trace("va desde "+desde+" hasta "+hasta);
var tw:Tween = new Tween(burbuja,"y",Regular.easeIn,desde,hasta,(velocidad*tiempoMaximo)+5,true);
tw.addEventListener(TweenEvent.MOTION_FINISH,_chauBurbuja);
addChild(burbuja);
}
private function _chauBurbuja(e:TweenEvent):void {
this.parent.removeChild(this);
BurbujasBack.burbujasActuales--;
}
}
Related
This is in the main class
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;
public class main extends MovieClip {
public var scene = 0;
public var _money = 0;
public var gain = 1;
public var clicks = 0;
public function main() {
addEventListener(Event.ENTER_FRAME, loop);
mainbtn.addEventListener(MouseEvent.CLICK, handler);
playbtn.addEventListener(MouseEvent.CLICK, playHandler);
}
var mainbtn:button = new button();
var playbtn:playbutton = new playbutton();
var playtxt:playtext = new playtext();
var cash:money_txt = new money_txt();
var scene0:MovieClip = new MovieClip();
var scene1:MovieClip = new MovieClip();
public function loop(e:Event):void {
if(scene == 0) {
addChild(scene0)
scene0.addChild(playbtn);
playbtn.x = 300;
playbtn.y = 200;
scene0.addChild(playtxt);
playtxt.x = 300;
playtxt.y = 100;
} else {
scene0.removeChild(playbtn);
scene0.removeChild(playtxt);
}
if(scene == 1) {
addChild(scene1);
scene1.addChild(mainbtn);
mainbtn.x = 300;
mainbtn.y = 200;
scene1.addChild(cash);
cash.text = 'Money: ' + _money.toString();
} else {
scene1.removeChild(mainbtn);
}
}
public function playclickHandler(e:MovieClip) {
scene = 1;
}
public function handler(e:MouseEvent):void {
_money += gain;
clicks++;
trace('yep');
}
public function playHandler(e:MouseEvent):void {
scene = 1;
}
}
}
And This is where the error would be
C:\Users\Slime\Desktop\Art-ish\game\main.as, Line 47, Column 10 1119: Access of possibly undefined property text through a reference with static type money_txt.
Thanks for helping if you can!
these should be defined as public
public var mainbtn:button = new button();
public var playbtn:playbutton = new playbutton();
public var playtxt:playtext = new playtext();
public var cash:money_txt = new money_txt();
public var scene0:MovieClip = new MovieClip();
public var scene1:MovieClip = new MovieClip();
also it is hard to tell if money_txt, playtext, playbutton and button are classes or MovieClip instances. Convention dictates that Classes should start with a capital letter and instances with lower.
update
The issue is that if button and playbutton are buttons and playtext and money_txt are MovieClips, you should instantiate them as such.
for example if you have
public var mainbtn:button = new button();
but there is no class with name of button, mainbtn will be null. What you may need to do is
public var mainbtn:Button;
public var cash:MovieClip;
and as a part of your main or some other function, assign the instances
mainbtn = this['button'];
cash = this['money_txt'];
you can check if this worked by checking trace(cash);, which will return null if the assignment did not work.
I should stress again though, it is hard to to know what exactly is going wrong without knowing what your setup is. I'm assuming money_txt and the other classes you are defining are not actually classes with their own linkage IDs, but buttons and movieclips inside the MovieClip or stage you are putting this code in.
I need to pre render animation, which I am creating by code in as3. I would like to save every frame of _debugBmp to *.png or *.bmp file, or create sprite sheet.
Is that possible?
Thank you for answer.
public class PerlinNoise extends Sprite
{
// premenne pre perlin noise
private var _baseX:Number = 45;
private var _baseY:Number = 5;
private var _numOctaves:uint = 3;
private var _randomSeed:int = 50;
private var _stitch:Boolean = true;
private var _fractalNoise:Boolean = false;
private var _channelOptions:uint = 1;
private var _grayScale:Boolean = true;
private var _offsets:Array = [];
private var _perlinBitmapData : BitmapData;
private var _debugBmp : Bitmap;
public function PerlinNoise()
{
_perlinBitmapData = new BitmapData(275, 50, true);
// oktavy perlin noisu
for(var i:int = 0; i < _numOctaves;i++) _offsets[i] = new Point(0,0);
_debugBmp = new Bitmap(_perlinBitmapData);
addChild(_debugBmp);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
// animacia perlin noisu
_offsets[1]['x'] += 1; // 2
_offsets[1]['y'] += 1/4;//1/4
// aplikacia perlin noisu
_perlinBitmapData.perlinNoise(_baseX, _baseY, _numOctaves, _randomSeed, _stitch, _fractalNoise, _channelOptions, _grayScale, _offsets);
}
}
I would recommend putting your code in an Adobe AIR application and then save the BitMapData out to files after each image is created in the onEnterFrame method. Once you have al your images than you could make a spritesheet out of them.
Every time I run my Flash program in a browser, the framerate drops from 30 to ~2 whenever I go to a different tab. Is there a way to stop the background framerate from tanking whenever the window isn't in focus? The framerate doesn't go down if I open a new window in the browser, even if the window covers the window with the Flash program. I'm also working in FlashDevelop if that helps. Thanks!
Unearthed some source code from 2014 and found what I ended up going with:
HUD.as:
public var idle:Boolean = false;
public var catchingUp:Boolean = false;
private var secondsIdle:int = 0;
stage.addEventListener(Event.DEACTIVATE, StartIdleMode);
stage.addEventListener(Event.ACTIVATE, RegainedFocus);
public function Update(sta:DisplayObjectContainer, save:SharedObject):void
{
//...The game logic that happens every frame...
if (secondsIdle < 0)
{
idle = true;
}
}
private function StartIdleMode(e:Event):void
{
var date:Date = new Date();
secondsIdle = date.valueOf();
}
private function RegainedFocus(e:Event):void
{
if (idle)
{
var diffDate:Date = new Date();
secondsIdle += -(diffDate.valueOf() / 2);
catchingUp = true;
}
}
public function CatchUp(sta:DisplayObjectContainer, save:SharedObject):void
{
var tempFrames:int = Math.floor(secondsIdle / 1000 * 30);
while (tempFrames < 0)
{
this.Update(sta, save);
tempFrames++;
}
idle = false;
catchingUp = false;
secondsIdle = 0;
}
Main.as:
private var gameTimer:Timer = new Timer(1000 / 30, 0);
private var hud:HUD;
public function Main():void
{
gameTimer.addEventListener(TimerEvent.TIMER, GameLoop);
gameTimer.start();
}
private function GameLoop(e:TimerEvent):void
{
if (!hud.idle)
{
hud.Update(stage, gameSystem.saveData);
}
if (hud.catchingUp)
{
hud.CatchUp(stage, gameSystem.saveData);
}
}
i separated one big file into multiple file to have it cleaned and i got a problem now.
I have my main.as, character.as, camera.as.
What i'm trying to do is access a variable from another class which i set later on that class. Ill show you what i mean.
From my main.as im loading each class and add them as child so it get displayed on screen.
public function buildGame()
{
var loadMap:Sprite = new nf_MapBuilder();
var xChar:Sprite = new nf_Character();
var xCam:Sprite = new nf_Camera();
var UserControl:nf_UserControl = new nf_UserControl();
addChild(loadMap);
addChild(xChar);
addChild(xCam);
addChild(UserControl);
}
Everything show on screen like it needed. Then it goes to my character.as:
package as3
{
import flash.display.Sprite;
import flash.events.Event;
public class nf_Character extends Sprite
{
public var character_pos:Array = new Array();
public var character_is_moving:Boolean = false;
public var character_x_dir:int = 0;
public var character_y_dir:int = 0;
public var character:hero = new hero();
public function nf_Character()
{
addEventListener(Event.ADDED_TO_STAGE,xCharLoad);
}
public function xCharLoad(e:Event)
{
character_pos = [2,2];
character.x=64*(character_pos[1]);
character.y=64*(character_pos[0]);
addChild(character);
}
}
}
There is the problem. I need to use those variable i set there in my character.as to use it in my camera.as:
package as3
{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.display.StageScaleMode;
import as3.nf_Character;
public class nf_Camera extends Sprite
{
private var xChar:nf_Character = new nf_Character();
//Camera variables
var stageW2:Number;
var stageH2:Number;
var view:Rectangle;
public function nf_Camera()
{
addEventListener(Event.ADDED_TO_STAGE,xCamGo);
}
public function xCamGo(e:Event):void
{
trace("Camera pos - " + xChar.x + " " + xChar.character.y);
view = new Rectangle(0,0,stage.stageWidth,stage.stageHeight)
stageW2 = stage.stageWidth / 2 - 32;
stageH2 = stage.stageHeight / 2 - 32;
addEventListener(Event.ENTER_FRAME,CamView);
}
public function CamView(e:Event):void
{
view.x = xChar.character.x - stageW2;
view.y = xChar.character.y - stageH2;
scrollRect = view;
}
}
}
When it was all in one big file it was ok i just had to set the variable in the class and acessing it trough every function but now im kinda confused. Anyone see how i could do this?
In short, I think you should subscribe to an event from your character in your main class which is fired whenever the character moves. In the handler for that event you could call a method on the camera to set it's position according to the current position of the character.
main.as
private var xChar:Sprite = new nf_Character();
private var xCam:Sprite = new nf_Camera();
public function buildGame()
{
var loadMap:Sprite = new nf_MapBuilder();
var UserControl:nf_UserControl = new nf_UserControl();
// listen for when the character has moved
xChar.addEventListener(MoveEvent.MOVED, characterMovedHandler);
addChild(loadMap);
addChild(xChar);
addChild(xCam);
addChild(UserControl);
}
private function characterMovedHandler(event:MoveEvent):void
{
xCam.setPosition(xChar.x, xChar.y);
}
nf_Character.as
public class nf_Character extends Sprite
{
public var character_pos:Array = new Array();
public var character_is_moving:Boolean = false;
public var character_x_dir:int = 0;
public var character_y_dir:int = 0;
public var character:hero = new hero();
public function nf_Character()
{
addEventListener(Event.ADDED_TO_STAGE,xCharLoad);
}
public function xCharLoad(e:Event)
{
character_pos = [2,2];
character.x=64*(character_pos[1]);
character.y=64*(character_pos[0]);
addChild(character);
}
public function xCharMoved()
{
// Dispatch a custom event when the character moves
dispatchEvent(new MovedEvent(MovedEvent.MOVED));
}
}
nf_Camera.as
public class nf_Camera extends Sprite
{
private var xChar:nf_Character = new nf_Character();
//Camera variables
var stageW2:Number;
var stageH2:Number;
var view:Rectangle;
public function nf_Camera()
{
addEventListener(Event.ADDED_TO_STAGE,xCamGo);
}
public function xCamGo(e:Event):void
{
trace("Camera pos - " + xChar.x + " " + xChar.character.y);
view = new Rectangle(0,0,stage.stageWidth,stage.stageHeight)
stageW2 = stage.stageWidth / 2 - 32;
stageH2 = stage.stageHeight / 2 - 32;
// Probably only need one enterframe either in your character class or main
//addEventListener(Event.ENTER_FRAME,CamView);
}
public function setPosition(x:Number, y:Number):void
{
view.x = xChar.character.x - stageW2;
view.y = xChar.character.y - stageH2;
scrollRect = view;
}
}
Out of interest, how are you moving the character?
You can pass your character class instance to your camera class instance as an argument of the constructor. You will then have a reference to the character inside the camera class and you can access it's variables
// Inside buildGame() in main.
var xChar:nf_Character = new nf_Character();
var xCam:nf_Camera = new nf_Camera(xChar);
// Inside nf_Camera
public function nf_Camera(char:nf_Character) {
xChar = char;
}
I am new and having an issue with the use of classes in as3.
I have created an array of objects in my main timeline
function badPlayer()
{
var bads:Array = new Array();
for (var i=0; i<5; i++)
{
var mc = new bman();
mc.name=["mc"+i];
bads.push(mc);
_backGround.addChild(mc);
mc.x = 100;
mc.y = 100;
trace (bads);
Baddies(_backGround.mc); //here I am trying to export mc to my class
}
}
Here is a snip-it from my class. My trace statement wont even output.
public class Baddies extends MovieClip
{
private var pistolSound:pistolShot = new pistolShot();
//private var mc = new mc();
private var _rotateSpeedMax:Number = 2;
private var _gravity:Number = .68;
private var _bulletSpeed:Number = 2;
private var _maxDistance:Number = 200;
private var _reloadSpeed:Number = 500; //milliseconds
private var _barrelLength:Number = 20;
private var _bulletSpread:Number = 5;
private var _isLoaded:Boolean = true;
private var _isFiring:Boolean = true;
private var _endX:Number;
private var _endY:Number;
private var _startX:Number;
private var _startY:Number;
private var _reloadTimer:Timer;
private var _bullets:Array = [];
private var _gun:MovieClip;
private var _enemy:MovieClip;
private var _yx:Number;
private var _yy:Number;
private var _pcos:Number;
private var _psin:Number;
private var _trueRotation:Number;
public function Baddies()
{
trace("working");
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
Basically I am trying to create several bad guys (bman) and have the same code apply to each of them. I have also tried to change the linkage name of bman to Baddies with no success.
There are a few thing that are very wrong with this code.
Baddies(_backGround.mc); //here I am trying to export mc to my class
This is a typecast, as already stated in the comments. By the way Baddies isn't a good name, because it plural. You probably want to create a new bad guy, which would be done with this line:
var baddie = new Baddies();
Now your constructor uses the stage variable. This won't work because the object isn't on the stage, therefore stage is null (it may works if you drag and drop an instance to the stage in the editor). So before using the stage you actually need to add the object to the stage:
public function Baddies() {
trace("new baddie created");
}
public function init(mc:MovieClip) {
mc.addChild(this); // display this baddie
trace("working");
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
And in the badPlayer function:
var baddie = new Baddies();
baddie.init(_backGround);