Starling AS3 IOS Splash Screen - actionscript-3

I have developed an app in AS3 and Starling to be ported to IOS. I have updated the Default.png image and this works great however my app takes a while to load and a black screen is shown for about 3-4 seconds.
I have looked everywhere for a solution but couldn't find any that work. Does someone have a working solution?
Many thanks

I'm not sure if there is a neater solution at the moment but what I do is add a bitmap of the default screen to the native flash stage. Then when Starling is ready I remove the bitmap.
So before you instantiate Starling, add the bitmap image to the stage (this will be the Flash stage)
public static var _splash:Bitmap;
//load or embed your bitmap//
addChild(_splash);
Then instantiate and start Starling. e.g.
myStarling = new Starling(Main, stage, null, null, Context3DRenderMode.AUTO, Context3DProfile.BASELINE);
myStarling.stage3D.addEventListener(starling.events.Event.CONTEXT3D_CREATE, function(e:flash.events.Event):void {
// Starling is ready!
myStarling.start();
});
In your root Starling class (in this example it's Main), use an ADDED_TO_STAGE listener and when this is triggered, remove the bitmap.
public function Main() {
addEventListener(starling.events.Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded ( e:starling.events.Event ):void {
StartUp._splash.parent.removeChild(StartUp._splash);
StartUp._splash = null;
}
In the example above the root document class is called 'StartUp'.

As described by docs there is Default.png used as splash screen in iOS.

Related

How drag a Sprite smoothly on-screen in actionscript

First of all, my question is basically the same as this one:
How to drag an image to move it smoothly on screen, with ActionScript?
I want my dragged Sprites to keep up with the mouse on-screen, smoothly, without lagging behind.
And I notice that in the old ActionScript 3 Cookbook from way-back-when that they used a similar solution for their DraggableSprite as was used in the above link. Namely, use the stage instance to listen for the MouseMove event and then read from the event.stageX and stageY properties.
I've done that.
But my Sprite still doesn't stay locked with the mouse cursor. It lags behind. I feel like I must be missing something. However, if the solution posted above (ie listen for stage's MouseMove and use event.stageX/Y) is still current and the problem I'm describing should not be occurring, please also let me know. Even though it's not supposed to work, I've tried event.updateAfterEvent() and it also doesn't seem to have any positive effect.
Any help or advice would be greatly appreciated.
Here's a simple example of how I've written the handlers. It should work as-is if pasted into a new project.
I should also add that I'm compiling this as a desktop application using Adobe AIR. Would the run time be a factor???
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
[SWF(width="1280", height="720", frameRate="30")]
public class test_drag extends Sprite {
private var testDragSprite:TestDragSprite;
public function test_drag() {
super();
graphics.clear();
graphics.beginFill(0x0000FF);
graphics.drawRect(0, 0, 1280, 720);
graphics.endFill();
testDragSprite = new TestDragSprite();
addChild(testDragSprite);
testDragSprite.addEventListener(MouseEvent.MOUSE_DOWN, testDragSprite_mouseHandler);
testDragSprite.addEventListener(MouseEvent.MOUSE_UP, testDragSprite_mouseHandler);
}
private function testDragSprite_mouseHandler(e:MouseEvent):void {
switch (e.type) {
case MouseEvent.MOUSE_DOWN: {
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
break;
}
case MouseEvent.MOUSE_UP: {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
break;
}
}
}
private function mouseMoveHandler(e:MouseEvent):void {
//-20 to keep the sprite centered on the mouse
testDragSprite.x = e.stageX - 20;
testDragSprite.y = e.stageY - 20;
//e.updateAfterEvent(); //strange effect, but doesn't solve the problem.
}
}
}
import flash.display.Sprite;
internal class TestDragSprite extends Sprite {
public function TestDragSprite() {
super();
graphics.lineStyle(1, 0xDDDDDD);
graphics.beginFill(0xFF0000);
graphics.drawRoundRect(0, 0, 40, 40, 12);
graphics.endFill();
}
}
There is always going to be a little lag, but:
The first two suggestions will make the most noticeable change to your code/performance.
Enable hardware graphics acceleration; edit your AIR application
descriptor file (xml) and set renderMode to Direct (or GPU). Consult
the Adobe Air help for details.
<!-- The render mode for the app (either auto, cpu, gpu, or direct). Optional. Default auto -->
<renderMode>direct</renderMode>
Use startDrag and endDrag to bypass your manual assignments in your mouseMoveHandler.
Replace :
testDragSprite.addEventListener(MouseEvent.MOUSE_DOWN, testDragSprite_mouseHandler);
testDragSprite.addEventListener(MouseEvent.MOUSE_UP, testDragSprite_mouseHandler);
With :
testDragSprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
testDragSprite.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
Add the new handlers:
private function mouseDown(e:MouseEvent):void {
testDragSprite.startDrag();
}
private function mouseUp(e:MouseEvent):void {
testDragSprite.stopDrag();
}
Increase your frame rate, but watch your CPU usage as your application/game becomes more complicated as you may need to lower it to allow your game logic enough time to complete between frames (otherwise you end up with what is called long frames, you can use the free Adobe Scout profiling tool to watch for these and lots of other things).
Add a frame rate HUD to your display so you can monitor when your actual framerate is lower than the requested framerate (this is for debugging)
If your game is 2d based, consider using the open-source Starling framework as all content is rendered directly by the GPU vs Flash's standard display list objects.
Just try the member function startDrag.
The Feathers UI library includes a drag-and-drop module. The answers above all use the 'old' display list which is not mobile-ready. If you want your code to work cross-platform you need a GPU framework (most popular is Starling on which Feathers is based, as well as most Adobe AIR games and Apps).

How do I add MovieClips to the stage while using AIR?

So I had been messing around in AS3 for awhile and had done some basic starting flash game things, but then found out I could use air to make them for mobile platforms. So I tried copying over what I had worked on before, but I am running into issues.
Right now I am just trying to find out how to add an instance of a MovieClip to the stage. I have an image converted to a MovieClip with it's own class so I can manipulate it later (such as moving around) and I have this in my Main class:
class Main extends MovieClip
{
var space:Background; //Background is the MovieClip
function Main():void
{
space = new Background(stage);
stage.addChild(space);
}
}
But when I run the program, the image doesn't show up. It works that way when I just had a standard AS3 program and I don't understand why it would be different when I am trying to use AIR. Any assistance would be greatly appreciated, thank you.
Put the word 'public' in front of 'class' in your first line.
Also remove the 'stage' from the first line in your constructor: make it space = new Background();
And do you really need 'stage' in the addChild statement? Unnecessarily verbose!
I can run this code through the AIR compiler (AIR SDK 3.8 or later):
package
{
import flash.display.MovieClip;
public class MAIN extends MovieClip
{
var space:Background;
public function MAIN()
{
space = new Background();
stage.addChild(space);
}
}
}
With an image inside the Background class it shows up fine. My 'Background' is a library symbol linked for Actionscript. If your 'Background' is pure Actionscript are you adding a graphic to it?
Rajneesh Gaikwad had the answer, I didn't have my Main class as my Document class.

as3 Air - AddChild works but is slooooooooooow

I am creating an app where when a button is pressed a very large picture is added to the stage (it is larger than the screen but can be dragged around by the user)
When the button is pressed the picture (well, movieClip) does come up and it able to be dragged fine, buttons inside it work.
The problem though is that there is a pause of about 6 seconds between the button press and the image appearing. I am using one .fla file for publishing and compiling (let's just call it Main.fla for now), and another one to hold all the graphics. The graphics are then added with this embed command:
[Embed (source = "assets/graphic.swf", symbol = "Content")]
private var Content:Class;
private var _content:*;
I have these lines where all the variables are declared (in between the class definition and the constructor function) I was under the impression that embedding it like this was equivalent to loading it at compile time. Is this true? What could be causing this lag when the button is pressed?
If I can't cut out the lag, another idea I had was to make some spinning circle or something to tell the user, "hey, don't worry. It's loading!"
If the slowness is at addChild you can add the asset to the stage much earlier and set it's visiblility to false, then when the button is clicked set it back to true. Obviously this is a small hack but might be sufficient for what you are doing.
var asset:MovieClip;
private function init():void
{
asset = new Content();
assset.visible = false;
addChild(asset);
button.addEventListener(MouseEvent.CLICK, onMouseClick);
}
private function onMouseClick(e:MouseEvent):void
{
asset.visible = true;
}
Embedding your SWF is probably not what is causing the delay.. or rather it would not likely be better if you imported the SWF into your FLA. Is this on a device? Chances are you would either have to devise a different way of loading your asset, or be satisfied with a loading animation.
If the main K size is coming from a large image, you could consider loading it in segments, starting with the part that is initially visible.

Simulating a MovieClip in pure as3

I am looking for a way to make a Flash movie clip (animation, like the ones created with Flash Pro CS), but purely in as3 - so I can import them into Prezi.
I have done a lot of as3 programming in Flash Builder with Flex projects and I have no background in how MovieClips work.
What I have already tried is extending a MovieClip class and trying to base the animation on Timers, this failed so I tried with ENTER_FRAME event (because flash animations are based on frames - so I thought...). But all this fails, only graphics drawn in the constructor are displayed - no animation happens. (As I wrote in the first paragraph I am testing this importing the swf into Prezi, opening it in a browser works as expected)
Is there any way to do it? Like listening to specific events?
Give sprite sheet a try. It's the best solution for animation in AS3, and also pretty simple to implement. for changing drawing, there are Timer and ENTER_FRAME event to do this.
Funny thing happened. I wanted to show you a sample code I was trying out (I already tried Sprite with ENTER_FRAME), that was not working. By accident I found a solution. It looks like you need to draw something in the first frame, or else the other frames won't work (in Prezi at least).
So here is the working code:
public class PreziTest extends Sprite{
private var radius:uint = 10;
public function PreziTest(){
addEventListener(Event.ENTER_FRAME, onEnterFrame);
onEnterFrame(null); // WITHOUT THIS IT WON'T WORK - YOU NEED TO DRAW SOMTHING IN THE FIRST FRAME
}
private function onEnterFrame(event:Event):void{
radius += 10;
if(radius > 200)
radius = 10;
graphics.clear();
graphics.beginFill(0xff0000);
graphics.drawCircle(radius, radius, radius);
}
}
Thanks for all your help!

Flash CS5 / AS3, after preloading main class constructor can't reference movieclips

I modified an Adobe Flash CS5 sample to create a swf with a preloader.
In my FLA I've two stopped frames:
In the first frame I only put this code (and a textfield showing percentage):
stop();
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onLoading(evt:ProgressEvent):void {
var loaded:Number = evt.bytesLoaded / evt.bytesTotal;
percent_txt.text = (loaded*100).toFixed(0) + "%";
};
function onComplete(event:Event):void {
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading);
this.loaderInfo.removeEventListener(Event.COMPLETE, onComplete);
gotoAndStop(2);
};
In the second frame I:
exported the Main class;
I have all the needed graphics assets on stage;
When I test the Adobe sample (it has no main class on frame 2, only a large image) anything works fine, but when I compile the modified version I get strange errors.
In the Main class constructor I reference three movieclips, eg. this way: myClip.alpha=0, but it seems now Flash can't see them anymore (they are null). Why?
How can I make this preloader work?
Thanks in advance.
When you say Main class, are you referring to a class you have set as the Document Class? If I'm following you correctly, the problem is likely that the Document Class is always instantiated on the first frame, so your instances on stage wouldn't yet exist.
It's not possible to have the Document Class wait to instantiate until later frames. You'll probably have to remove your class from the Document Class assignment in order to create your instance on frame 2. At that point, you can pass a reference to your movieclips or stage through to the constructor of your class.