Ok, i want to add a series of images, and then be able to drag and drop each one of them. I have all of my images embedded in a class Images. s0,s1,s2 are instances of image classes. Now this is what i've done
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
function init(e:Event = null):void
{
var a:Array = new Array();
var imageContainer:Sprite = new Sprite;
var imgClass:Images = new Images();
for (var i:int = 0; i < 9; i++) {
a[i].push(imgClass.(s+String(i)));
imageContainer.addChild[a[i]];
}
stage.addChild(imageContainer);
imageContainer.addEventListener(MouseEvent.MOUSE_UP, takeIt);
imageContainer.addEventListener(MouseEvent.MOUSE_DOWN, dropIt);
function takeIt(event:MouseEvent) {
event.currentTarget.startDrag();
}
function dropIt(event:MouseEvent) {
event.currentTarget.stopDrag();
}
}
}
I have rewritten your code a bit, but I have not been able to test it sorry.
The first think I notices is that your init method was inside your Main method. Then your takeIt and dropIt method were inside the init. I am not to sure if that would actually work, but I have fixed it up in the code below.
In my code I assume that the image instances you have in the Images class are instances of Bitmap. This means that inside the for loop I had to add each one to a Sprite so you have access to startDrag and stopDrag. I listen for the MOUSE_DOWN event on each image, and set the image to the selectedImage var and do startDrag. I also listen for the MOUSE_UP on the stage, and drop the currentImage.
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var selectedImage:Sprite;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var imageContainer:Sprite = new Sprite();
var imgClass:Images = new Images();
for (var i:int = 0; i < 9; i++)
{
var imgInstance:Bitmap = imgClass['s' + i.toString()] as Bitmap;
var imgSprite:Sprite = new Sprite();
imgSprite.addChild(imgInstance); // Put image in a sprite so we can use startDrag on it.
imageContainer.addChild(imgSprite);
imgInstance.addEventListener(MouseEvent.MOUSE_DOWN, img_mouseDownHandler);
}
addChild(imageContainer);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler);
}
private function dropSelectedImage():void
{
if (selectedImage)
{
selectedImage.stopDrag();
}
}
private function img_mouseDownHandler(e:MouseEvent):void
{
dropSelectedImage();
selectedImage = e.currentTarget as Sprite;
selectedImage.startDrag();
}
private function stage_mouseUpHandler(e:MouseEvent):void
{
dropSelectedImage();
}
}
}
To build your images in a loop you could also just manually build the array, then loop through the array like so:
var images:Array = [imgClass.s0, imgClass.s1, imgClass.s2, imgClass.s3, imgClass.s4, imgClass.s5, imgClass.s6, imgClass.s7, imgClass.s8];
for each (var img:Bitmap in images)
{
var imgSprite:Sprite = new Sprite();
imgSprite.addChild(img); // Put image in a sprite so we can use startDrag on it.
imageContainer.addChild(imgSprite);
imgInstance.addEventListener(MouseEvent.MOUSE_DOWN, img_mouseDownHandler);
}
Hope this help. Feel free to ask me to elaborate or explain anything else in the comments.
Related
Here's what I have so far. The background goes green (the colour of the Page), but I'd expect a purple ContentView with some text inside to fill the page, too.
Is there anything further I'm missing?
import { on, run, launchEvent } from "tns-core-modules/application";
import { Frame } from "tns-core-modules/ui/frame/frame";
import { ContentView } from "tns-core-modules/ui/content-view/content-view";
import { TextBase } from "tns-core-modules/ui/text-base/text-base";
import { Page } from "tns-core-modules/ui/page/page";
on(launchEvent, (data) => {
const frame = new Frame();
const page = new Page();
page.backgroundColor = "green";
const contentView = new ContentView();
const textBase = new TextBase();
contentView.height = 100;
contentView.width = 100;
contentView.backgroundColor = "purple";
textBase.text = "Hello, world!";
contentView._addView(textBase);
page.bindingContext = contentView;
frame.navigate({ create: () => page });
data.root = page; // Incidentally, should this be the frame or the page?
});
run();
You are almost on track, you just need slight modification on your code.
import { on, run, launchEvent } from 'tns-core-modules/application';
import { Frame } from 'tns-core-modules/ui/frame/frame';
import { ContentView } from 'tns-core-modules/ui/content-view/content-view';
import { TextField } from 'tns-core-modules/ui/text-field';
import { Page } from 'tns-core-modules/ui/page/page';
run({
create: () => {
const frame = new Frame();
frame.navigate({
create: () => {
const page = new Page();
page.backgroundColor = "green";
const contentView = new ContentView();
const textField = new TextField();
contentView.height = 100;
contentView.width = 100;
contentView.backgroundColor = "purple";
textField.text = "Hello, world!";
contentView.content = textField;
page.content = contentView;
return page;
}
});
return frame;
}
});
You don't have to wait for launch event, you could set the root frame in run method itself.
In your code, you were creating the frame but never adding it to root UI element or mark the frame itself as root element
It's recommended to use .content to add child for a ContentView / Page as they are originally designed to hold one child element only.
Use TextField / TextView for input text, TextBase is just a base class.
It seems to me that you try to overcomplicate. You can replace XML with code just by implementing createPage method - Create a page via code.
I just modified default NS + TypeScript Playground template to operate without XML - NS + TypeScript template without XML.
I think you can't leave run as empty as it is expecting an entry to start the app. From {NS} website,
You can use this file to perform app-level initializations, but the
primary purpose of the file is to pass control to the app's root
module. To do this, you need to call the application.run() method and
pass a NavigationEntry with the desired moduleName as the path to the
root module relative to your /app folder.
if you look for run code in "tns-core-modules/application"
function run(entry) {
createRootFrame.value = false;
start(entry);
}
exports.run = run;
and
function start(entry) {
if (started) {
throw new Error("Application is already started.");
}
started = true;
mainEntry = typeof entry === "string" ? { moduleName: entry } : entry;
if (!androidApp.nativeApp) {
var nativeApp = getNativeApplication();
androidApp.init(nativeApp);
}
}
I have a sprite that I can drag around on screen. I want to be able to drag this sprite into an area (box). As it stands now I can only drop the sprite into the box, but when I drag it directly inn, the program crashes.
I have debugged in FlashDevelop using the Adobe Flash debugger. When I place the sprite into the box the debugger points to this line of code in the DistanceJoint.hx file:
if(b1.space!=space||b2.space!=space)throw "Error: Constraints must have each body within the same space to which the constraint has been assigned";
I think I understand what I have to do, but I am having a hard time finding a way to make a proper exception in my Drag class.
My idea is to break the mouseJoint in the Drag class when the collideLightBox function in Playstate is used. But I do not know how to do that, or if it is the right idea. Please help.
Relevant code:
class Drag extends FlxGroup {
var mouseJoint:DistanceJoint;
public inline function registerPhysSprite(spr:FlxNapeSprite)
{
MouseEventManager.add(spr, createMouseJoint);
}
function createMouseJoint(spr:FlxSprite)
{
var body:Body = cast(spr, FlxNapeSprite).body;
mouseJoint = new DistanceJoint(FlxNapeState.space.world, body,
new Vec2(FlxG.mouse.x, FlxG.mouse.y),
body.worldPointToLocal(new Vec2(FlxG.mouse.x, FlxG.mouse.y)),
0, 0);
mouseJoint.space = FlxNapeState.space;
}
override public function update():Void
{
super.update();
if (mouseJoint != null)
{
mouseJoint.anchor1 = new Vec2(FlxG.mouse.x, FlxG.mouse.y);
if (FlxG.mouse.justReleased)
{
mouseJoint.space = null;
}
}
}
}
class PlayState extends FlxNapeState {
override public function create()
{
super.create();
bgColor = FlxColor.BLACK;
napeDebugEnabled = true;
var light = new Light(10, 10);
var box = new Box(100, 100);
var drag:Drag;
createWalls(1, 1, 1024, 768, 10, new Material(1, 1, 2, 1, 0.001));
add(light);
add(box);
drag = new Drag();
add(drag);
drag.registerPhysSprite(light);
light.body.velocity.y = 200;
FlxNapeState.space.listeners.add(new InteractionListener(
CbEvent.BEGIN,
InteractionType.COLLISION,
Light.CB_TYPE,
Box.CB_TYPE,
collideLightBox));
}
function collideLightBox(callback:InteractionCallback)
{
var light:Light = cast callback.int1.castBody.userData.sprite;
light.kill();
}
}
class Light extends FlxNapeSprite {
public static var CB_TYPE(default, null) = new CbType();
public function new(x:Float, y:Float)
{
super(x, y);
makeGraphic(10, 10, FlxColor.TRANSPARENT);
var radius = 5;
drawCircle(5, 5, radius, FlxColor.WHITE);
createCircularBody(radius);
body.cbTypes.add(CB_TYPE);
body.userData.sprite = this;
}
}
class Box extends FlxNapeSprite {
public static var CB_TYPE(default, null) = new CbType();
public function new(x:Float, y:Float)
{
super(x, y);
makeGraphic(100, 50, FlxColor.GREEN);
createRectangularBody(width, height);
body.cbTypes.add(CB_TYPE);
body.type = BodyType.STATIC;
}
}
Give Drag a function like "destroyConstraints()" and set mouseJoint.space = null inside that function. In collideLightBox, call drag.destroyConstraints().
I am just starting to learn actionscript, and to help get used to the syntax, I am challenging myself to make a simple game where you are a circle that shoots falling blocks.
For some reason every time I try to add a keyboard event listener the game doesn't run.
Here is my player file.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Player extends Sprite
{
//Variables
private var playerRadius:Number = 50;
private var playerX:Number = 5;
private var playerY:Number = 5;
private var speed:Number = 0;
private var xvel:Number = 0;
public function Player()
{
init();
//Drawing
drawPlayer();
//Event Listeners
this.addEventListener(Event.ENTER_FRAME, updatePlayer);
stage.addEventListener(KeyboardEvent.KEY_DOWN, controlPlayer);
}
//Update
public function updatePlayer(event:Event):void{
this.x ++;
}
//Draw
private function drawPlayer():void{
graphics.beginFill(0xFF0000);
graphics.drawCircle(10,10,50);
graphics.endFill();
}
//Control
public function controlPlayer(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.RIGHT) {
speed = 5;
}
}
}
}
With this code I just get a white screen, but if I comment out
stage.addEventListener(KeyboardEvent.KEY_DOWN, controlPlayer);
it works, but I don't have control of the player.
I'd appreciate any and all help!
Using your code I was able to figure out your issue which ultimately turned out to be a couple problems with your code. I'm surprised you were not seeing the following error in the Flash 'Output' Panel when you tested the application:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Player()
at Player_fla::MainTimeline/frame1()
The first issue is that when you create an object of the type Player, it isn't yet added to the Stage, so it does not yet have access to the stage object.
Once the player object is added to the Stage, only then will you be able to add the listener for keyboard events to the stage; however, for this to happen, your Player class needs to be made aware of the fact that an instance of it was added to the stage so that it knows exactly when it should register the keyboard event listener.
Here is an updated version of your code that should resolve these issues:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Player extends Sprite
{
//Variables
private var playerRadius:Number = 50;
private var playerX:Number = 5;
private var playerY:Number = 5;
private var speed:Number = 0;
private var xvel:Number = 0;
public function Player()
{
init();
//Drawing
drawPlayer();
//Event Listeners
this.addEventListener(Event.ENTER_FRAME, updatePlayer);
this.addEventListener(Event.ADDED_TO_STAGE, initKeyboardListener);
}
public function initKeyboardListener(event:Event) {
stage.addEventListener(KeyboardEvent.KEY_DOWN, controlPlayer);
}
//Update
public function updatePlayer(event:Event):void{
this.x++;
}
//Draw
private function drawPlayer():void{
graphics.beginFill(0xFF0000);
graphics.drawCircle(10,10,50);
graphics.endFill();
}
//Control
public function controlPlayer(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.RIGHT) {
this.speed = 5;
}
}
} // end class
} // end package
For all of this to work, don't forget to add the player object to the stage. I can only assume you have done this since you haven't shared any code showing where you use the Player class, but here is an example of what I am referring to:
import Player;
var player:Player = new Player();
stage.addChild(player);
Also, the keyboard listener simply alters the speed variable; however the speed variable hasn't been implemented anywhere else in your code, so you won't see the difference in the GUI until this is fixed. I verified that all the listeners were working as they should using trace statements.
I read in this article http://www.adobe.com/devnet/flex/articles/flex-mobile-performance-checklist.html that I should not initialize a View's appearance in a creationComplete handler. Instead, I should change view's appearance in an overridden data setter.
The section in the article is:
Override the data setter instead of using bindings or initializing a View's appearance in a creationComplete handler
1-First, I would like to know if I got this right by doing the following:
//My code is loading a set of images and adding them in a View.
//On creationComplete of the View I am adding the images in case this is the first time
//the view is shown. In case the view has been already accessed I use the data:
protected function view1_creationCompleteHandler(event:FlexEvent):void
{
if(!data) //On first creation of the view I create the data object
{
data = new Object();
data.imageArray = new Array(); //set an array that will cache my images.
for(var i:int = 0; i<36;i++)
{
var img:Image = new Image();
img.source = 'assets/0'+i.toString()+'.png';
container.addElement(img);
(data.imageArray as Array).push(img);//Override the data for next time!
}
}
else//Next time use the save images
{
for(var ix:int = 0; ix<(data.imageArray as Array).length;ix++)
{
container.addElement((data.imageArray as Array)[ix]);
}
}
}
If I am doing this correctly, I would like to know which approach is best. The above one, or the next one I am going to show which uses the images contentLoader with caching and queuing enabled with a ContentCache:
protected function view1_creationCompleteHandler(event:FlexEvent):void
{
{
for(var i:int = 0; i<36;i++)
{
var img:Image = new Image();
img.contentLoader = ldr;
img.contentLoaderGrouping = 'gr1';
img.source = 'assets/0'+i.toString()+'.png';
container.addElement(img);
}
}
<fx:Declarations>
<s:ContentCache id="ldr" enableQueueing="true"
maxActiveRequests="1" maxCacheEntries="36"/>
</fx:Declarations>
Also if someone could tell me what is the contentLoaderGrouping for. I would be very grateful.
Thanks a lot!!!
PS:By the way both approaches work. The first approach is instant while the second approach shows the images beeing added in a very smooth way which actually gives a cool effect.
Neither. The point of the suggestion was to NOT alter the displaylist after creationComplete, which requires an additional update cycle. Instead you should inject the data property when you push your view on the stack, and initiate your changes in the setter. Using the ContentCache has nothing to do with it (and can sometimes cause additional overhead if not used correctly).
override public function set data(value:Object):void
{
super.data = value;
//this was poorly optimized, so I made it
//a little better...
var imageArray:Array = (value == null || value.imageArray == null)?
null : value.imageArray as Array;
if(imageArray == null) //On first creation of the view I create the data object
{
imageArray = new Array(36); //set an array that will cache my images.
for(var i:int = 0; i<36;i++)
{
var img:Image = new Image();
img.source = 'assets/0'+i.toString()+'.png';
container.addElement(img);
imageArray[i] = img;
}
super.data = {imageArray:imageArray}
}
else//Next time use the save images
{
var n:int = imageArray.length;
for (var j:int = 0; j < n; j++)
{
container.addElement(IVisualElement(imageArray[j]));
}
}
}
EDIT
I was mistaken about when the data property is set during the view life-cycle.
Here is how it works:
So you are correct that container would be null at that point. I was going to write up an example for you, but I'm having trouble figuring out what your end goal is here. Is there a specific reason you are storing the images on the data property? I think what you might actually want to do is this:
private var _data:Object = {cache: new ContentCache()};
protected function show_clickHandler(event:MouseEvent):void
{
this.navigator.pushView(views.MyView, _data);
}
And in the view...
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="MyView">
<fx:Script>
<![CDATA[
import spark.components.Image;
import spark.core.ContentCache;
override protected function createChildren():void
{
super.createChildren();
//you might want to do a sanity first check to make sure the
//data was passed in correctly...
var cache:ContentCache = ContentCache(this.data.cache);
for(var i:int = 0; i < 36; i++)
{
var img:Image = new Image();
img.contentLoader = cache;
img.source = 'assets/0' + i.toString() + '.png';
container.addElement(img);
}
}
]]>
</fx:Script>
<s:VGroup id="container" />
</s:View>
I am loading images for a game before the game begin. So the main function sends links of images to an image loading object. This is what happen in my image loading object when I do image.load(link) in my main :
public function charge(str:String, img_x:int, img_y:int, layer:Sprite):void
{
trace("load");
urlRequest = new URLRequest(str);
loaderArray[cptDemande] = new Loader();
loaderArray[cptDemande].contentLoaderInfo.addEventListener(Event.COMPLETE, loading_done);
loaderArray[cptDemande].load(urlRequest);
posX[cptDemande] = img_x;
posY[cptDemande] = img_y;
layerArray[cptDemande] = layer;
cptDemande++;
}
The parameters img_x:int, img_y:int and layer:Sprite are related to displaying the images afterward. I am using arrays to be able to add the images to the stage when the loading is all done.
The event listener fire this function :
public function loading_done(evt:Event):void
{
cptLoaded++;
evt.currentTarget.contentLoaderInfo.removeEventListener(Event.COMPLETE, loading_done);
if((cptDemande == cptLoaded) && (isDone == true))
{
afficher();
}
}
what I want is to be able to target the good loader to remove the event listener. What I am currently using(evt.currentTarget) doesn't work and generate an error code :
1069 Property data not found on flash.display.LoaderInfo and there is no default value
Tracing evt.currentTarget shows that currentTarget is the LoaderInfo property. Try updating your code as follows:
public function loading_done(evt:Event):void
{
cptLoaded++;
// Current target IS the contentLoaderInfo
evt.currentTarget.removeEventListener(Event.COMPLETE, loading_done);
//evt.currentTarget.contentLoaderInfo.removeEventListener(Event.COMPLETE, loading_done);
if((cptDemande == cptLoaded) && (isDone == true))
{
afficher();
}
}
Just a wee tip for you while I'm at it, you could make life a lot easier for yourself by storing all the properties of your images on an Object and then pushing these onto a single Array, rather than managing a separate Array for each property.
Something like this:
private var loadedImages:Array = new Array();
public function charge(str:String, img_x:int, img_y:int, layer:Sprite):void
{
var urlRequest:URLRequest = new URLRequest(str);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loading_done);
loader.load(urlRequest);
var imageData:Object = { };
imageData.loader = loader;
imageData.posX = img_x;
imageData.posY = img_y;
imageData.layer = layer;
// Now we have a single Array with a separate element for
// each image representing all its properties
loadedImages.push(imageData);
cptDemande++;
}