I have a custom class that embeds a movie clip. When I try to use parent class's such as gotoAndStop(). It ignores the method. I would like to know what I am doing wrong. below is my code. I call the method in my constructor Thanks!
game object class
package com.objects
{
import com.eapi.IGameObject;
import flash.display.MovieClip;
/**
* ...
* #author Anthony Gordon
*/
public class GameObject extends MovieClip implements IGameObject
{
public function GameObject()
{
}
}
}
hero class. gotoAndStop() in the constructor
package com.objects
{
import flash.display.MovieClip;
import flash.events.*;
/**
* ...
* #author Anthony Gordon
*/
[Embed(source='../../../bin/Assets.swf', symbol='Hero')]
public class Hero extends GameObject
{
private var aKeyPress:Array;
private var jumpDisabled:Boolean = false;
public function Hero()
{
gotoAndStop(1) ///<----------------Doesnt stop. Just keeps playing
wY = 150;
wX = 90;
speed = .5;
aKeyPress = new Array();
TheGame.sr.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
TheGame.sr.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
}
private function keyDownListener(e:KeyboardEvent):void {
//trace("down e.keyCode=" + e.keyCode);
aKeyPress[e.keyCode]=true;
}
private function keyUpListener(e:KeyboardEvent):void {
//trace("up e.keyCode=" + e.keyCode);
aKeyPress[e.keyCode]=false;
}
override public function UpdateObject():void
{
Controls();
updatePosition();
}
private function Controls():void
{
if (aKeyPress[38])//Key press up
;//dy -= speed;
else if (aKeyPress[40])//Key press down
dy += speed;
if (aKeyPress[37])//left
{
dx -= speed;
}
else if (aKeyPress[39])//Right
{
dx += speed;
}
if (aKeyPress[32]){//space
jump();
}
}//End Controls
private function jump():void
{
if (!jumpDisabled)
{
if (onGround)
{
gotoAndStop(10);
dy = -15;
jumpDisabled = true;
}
}
else
{
jumpDisabled = false;
}
}
}
}
The problem is that when you embed a swf of a movie clip actually what you got is a movieclip with and added child Loader who get inside the real movieclip.
You must try something like
var aMovie:MovieClip;
Loader(this.getChildAt(0)).getLoaderInfo.addListener(Event.Complete, methodEventCompleteName);
function methodEventCompleteName(e:Event)
{
aMovie = MovieClip(Loader(this.getChildAt(0)).content);
}
I really suggest to try learn to use SWC files. I export him on flash cs5 IDE then add on a lib folder into my proyect, then in flashdeveloper ide right click into the SWC file and check the "add to library" option. Inside a class can directly instantiate the movieClip name into the SWC file like this:
var aMovie:MovieClip = new MyMovieClipFromSWC() as MovieClip;
and it's done :D
Hope this help to someone... because I spend like 3 weeks trying to figured out and finally get it... there isn't enough info about it...
You should add a listener to know when its added to stage and initialize your clip there.
public function Hero() {
if ( stage ) _init( );
else addEventListener(Event.ADDED_TO_STAGE, _init );
}
private function _init( e:Event = null ):void {
removeEventListener( Event.ADDED_TO_STAGE, _init );
gotoAndStop(1) ///<----------------Doesnt stop. Just keeps playing
wY = 150;
wX = 90;
speed = .5;
aKeyPress = new Array();
TheGame.sr.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
TheGame.sr.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
}
Also, i think this reading Order of operations by senocular would be rather interesting.
It explains frame execution in timeline objects and object creation.
Figure it out. I forgot that my MovieClip was encapuslated in another movieclip. So i had to call the movieclip that was inside of the Hero symbol
I noticed you're still using
[Embed(source='../../../bin/Assets.swf', symbol='Hero')]
Since you're using flash develop (but same goes for flex) there's no reason you can't use SWCs
Although I can't tell with 100% certainty that that is what is causing this issue, once you start using SWCs it will make a world of difference.
Not only will you no longer need to use the embed code, things like gotoandplay and extending classes will work better, and you will be able to see the classes and objects within.
SWFs are not meant to be used this way, and sooner you stop using them for things they are not meant for, the the easier it will be.
Related
I'm using starling framework under flex AS3 project.
I've Sprite which named Bird and it use Altas for animation.
My problem is, there is two bird in my screen and both of them flaps at the same time. I want to flap asynchronous.
How can I do this, can I give a start frame number each of them ?
Thanks..
public class Bird extends Sprite
{
private var bird_mc:MovieClip;
public function Bird(startFrame:Number = 0)
{
super();
this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
createBird_mc();
}
private function createBird_mc():void
{
bird_mc = new MovieClip(Assets.getAtlas().getTextures("Bird_"), 16);
bird_mc.x = Math.ceil(-bird_mc.width/2);
bird_mc.y = Math.ceil(-bird_mc.height/2);
starling.core.Starling.juggler.add(bird_mc);
this.addChild(bird_mc);
}
}
the solution is setting activeframe before add to stage.
im a newbie in flex. Im have a question :)
I have
[Bindable]
private var model:AlgorithmModel = new AlgorithmModel();
private var serviceProxy:Algorithm = new Algorithm( model );
In MXML
private function Show():void
{
// now model.Solve_SendResult = null
while(i<model.Solve_SendResult.length) //
{
Draw(); //draw cube
}
}
private function Solve_Click():void
{
//request is a array
Request[0] = 2;
Request[1] = 2;
Request[2] = 3;
serviceProxy.Solve_Send(request);
Show();
}
<s:Button x="386" y="477" label="Solve" click="Solve_Click();"/>
And when i call serviceProxy.Solve_Send(request); with request is array and i want use model.Solve_SendResult in my code flex to draw many cubes use papervison3d but in the first time i received model.Solve_SendResult = null . But when I click again then everything OK.
Anyone help me? Thanks?
The model.Solve_SendResult object contains a result of the executed serviceProxy.Solve_Send(request) method. The Solve_Send will be executed asynchronously and as a result, at the moment when you fire the show method the Solve_SendResult object may be still null.
As a solution, you can use the following:
Create a custom event
package foo
{
import flash.events.Event;
public class DrawEvent extends Event
{
public static const DATA_CHANGED:String = "dataChanged";
public function DrawEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
In your Algorithm class define the following:
[Event(name=DrawEvent.DATA_CHANGED, type="foo.DrawEvent")]
public class Algorithm extends EventDispatcher{
//your code
In the Solve_SendHandler method of the Algorithm class add the following
public virtual function Solve_SendHandler(event:ResultEvent):void
{
dispatchEvent(new DrawEvent(DrawEvent.DATA_CHANGED));
//your code
}
In your MXML class create onLoad method and add an event listener to an instance of the Algorithm class as it shown below:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="onLoad()">
public function onLoad():void
{
serviceProxy.addEventListener(DrawEvent.DATA_CHANGED, onDataChanged);
}
private function onDataChanged(event:DrawEvent):void{
while(i<model.Solve_SendResult.length) //
{
Draw(); //draw cube
}
}
make the following changes in the Solve_Click() method:
private function Solve_Click():void
{
//request is a array
Request[0] = 2;
Request[1] = 2;
Request[2] = 3;
serviceProxy.Solve_Send(request);
}
That is it! So, basically the code above do the following: you added a listener to your service (algorithm class), and the listener is listening for the DrawEvent.DATA_CHANGED event. The DrawEvent.DATA_CHANGED will be dispatched when your client receive a result of the Solve_Send invocation. Thus, the onDataChanged will draw your cube or do whatever you want :)
The approach above is basic and you have to know how events work in flex and how you can deal with it. Additional information is available here:
http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_3.html
http://livedocs.adobe.com/flex/3/html/help.html?content=events_07.html
Regards,
Cyril
This is the code that i compiled using mxmlc the free flex sdk. It compiled and i ran it. Helloworld not displayed
package
{
import org.flixel.*;
public class PlayState extends FlxState
{
override public function create():void
{
add(new FlxText(0,0,100,"Hello, World!")); //adds a 100x20 text field at position 0,0 (upper left)
}
}
}
Expected output is = HelloWorld. But is not displayed
You'll need to provide an FlxGame subclass too. It may be easiest for you to take an existing Flixel project that works out of the box such as AdamAtomic's HelloWorld and modify it to suit your needs.
Try this instead:
package
{
import org.flixel.*;
public class HelloWorld extends FlxState
{
private var title_text:FlxText;
public function HelloWorld() { }
override public function create():void
{
title_text = new FlxText(20, 0, 300, 'Lunarium');
title_text.setFormat(null, 50, 0xffffffff, "center");
add(title_text);
}
override public function update():void
{
if(FlxG.keys.X)
{
FlxG.fade(0xff131c1b, 1, onFade);
}
super.update();
}
protected function onFade():void
{
//FlxG.switchState(new SmallPlay2());
}
}
}
There are plenty of tool tips to help you figure out what the different settings mean. I found flixel rather easy to use coming from a Multimedia Fusion 2 background.
Enjoy!
Is there any workaround to create submenu in a flex context menu other than stopping right click from javascript.
Regards,
Hi Frank,
Yes, I want to create submenus in a context menu. Can you help me here.
Regards,
Hi Frank,
I need the context menu for the application not for datagrid.
In my initial question the phrase "other than stopping right click from javascript" means
"catch the right click in html, call a javascript function and over js call a as function."
The project that you have specified does the above procedure. I don't want to use this
procedure. Is there any other way for achieving submenus in a flex context menu. Could you
please tell me if so..
Regards,
Arvind
Yes, there is.
I don't know, what you exactly mean with this:
other than stopping right click from
javascript.
But, if you want to create a entry in submenu, do this:
//Instance of my own class
private var myContext:myContextMenu = new myContextMenu();
application.contextMenu = myContext.myContextMenu;
//Here is the Class:
package com.my.components
{
/* ////////////////////////////////////////////
///// My Context MenĂ¼ /////////////////////
///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//to use: //
// private var myContext:MyContextMenu = new MyContextMenu(); //
// init() in creationComplete //
// application.contextMenu = myContext.myContextMenu; //
////////////////////////////////////////////////////////////////////////////// */
import flash.display.Sprite;
import flash.events.ContextMenuEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.text.TextField;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuBuiltInItems;
import flash.ui.ContextMenuItem;
public class MyContextMenu extends Sprite
{
public var myContextMenu:ContextMenu;
private var menuLabel:String = String.fromCharCode(169)+" My Company GmbH";
public function MyContextMenu()
{
myContextMenu = new ContextMenu;
removeDefaultItems();
addCustomItems();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, menuSelectHandler);
super();
}
private function removeDefaultItems():void
{
myContextMenu.hideBuiltInItems();
var defaultItems:ContextMenuBuiltInItems = myContextMenu.builtInItems;
defaultItems.print = true;
}
private function addCustomItems():void
{
var item:ContextMenuItem = new ContextMenuItem(menuLabel);
myContextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemSelectHandler);
}
private function menuSelectHandler(event:ContextMenuEvent):void
{
}
private function menuItemSelectHandler(event:ContextMenuEvent):void
{
navigateToURL(new URLRequest('http://www.my-company.de'));
}
private function createLabel():TextField
{
var txtField:TextField = new TextField();
//txtField.text = textLabel;
txtField.text = "RightClickHere";
return txtField;
}
}
}
Have fun
EDIT:
There is an interesting project here. They catch the right click in html, call a javascript function and over js call a as function.
Unfortunately, the limitation of FP or NativeMenu APi allowed just on level contextmenu. Read here
Frank
When I debug my code nothing appears on screen. I've rechecked the code and consulted with others yet nothing appears. My html template is fine.
package {
import flash.display.Sprite;
import flash.events.*;
public class asgnv2 extends Sprite
{
var lineY = 0;
public function asgnv2()
{
stage.addEventListener(Event.ENTER_FRAME, update);
graphics.lineStyle(1);
}
function update(e){
graphics.clear();
graphics.moveTo(0 ,lineY);
graphics.lineTo(100, lineY);
lineY+=0.5;
}
}
}
unless asgnv2 is Document class, it is not going to work, as you are registering ENTER_FRAME event on the stage inside the constructor of asgnv2. A DisplayObject can not access stage property until it is added to Stage Display List. So try the following. public function asgnv2(){
this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
graphics.lineStyle(1);
}
private function onAdded(e:Event):void {
stage.addEventListener(Event.ENTER_FRAME, update);
this.removeEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function update(e:Event):void{
//do the stuff
}