targeting sprites from a method in the document class - null object reference - class

I am trying to code a flash app entirely in the document class. I am using GestureWorks with a touch screen. When a user essentially presses a button it calls a method that should hide a specific graphic but not the graphic they touched.
Essentially I need a way to refer to a graphic on the screen using a method besides 'e.target'.
I am receiving this error: Error #1009: Cannot access a property or method of a null object reference.
//This code works
private function photo1SpriteFlickHandler(e:GestureEvent):void {
var openTween:Tween = new Tween(e.target, "x", Strong.easeOut, 232, 970, 5, true);
}
//this code gives me a null object reference
private function photo1SpriteFlickHandler(e:GestureEvent):void {
var openTween:Tween = new Tween(photo1Sprite, "x", Strong.easeOut, 232, 970, 5, true);
}
//photo1Sprite has already been programatically added to the screen as so:
var photo1Sprite = new TouchSprite();
var photo1Loader=new Loader();
photo1Loader.load(new URLRequest("media/photos1/photo1.jpg"));
photo1Loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderComplete);
photo1Sprite.x = 232;
photo1Sprite.y = 538;
photo1Sprite.scaleX = .3;
photo1Sprite.scaleY = .3;
photo1Sprite.blobContainerEnabled = true;
photo1Sprite.addEventListener(TouchEvent.TOUCH_DOWN, startDrag_Press);
photo1Sprite.addEventListener(TouchEvent.TOUCH_UP, stopDrag_Release);
photo1Sprite.addChild(photo1Loader);
addChild(photo1Sprite);
It can access photo1Sprite as 'e.target' when the button click happens on the photo1Sprite.
The problem happens when to click one button (not photo1Sprite) and have it effect photo1Sprite.
So I can make photo1Sprite react if my method is attached to it directly using 'e.target' but not if I am trying to call it from a method that was called from another element on the screen.

I'm not sure what the Tween class constructor expects as it first argument. Is it a Sprite instance or is it the name of a Sprite instance? In any case make sure that - in the context of photo1SpriteFlickHandler - photo1Sprite is 1) defined! and 2) refers to the correct thing.

Related

How do you add an if statement to a TimerComponent in Flame Game Flutter?

I'm trying to add code to my Flame Game to check if a list isn't empty and if it isn't, then send it to a function. However, I'm receiving an error on the if statement that says "Expected an identifier". How do I change my code to run an if statement here? Additionally, how would I cancel the Timer after it runs?
var instructions = [];
myGame(){
add(
TimerComponent(period: 2, repeat: true, onTick: () =>
if(instructions != null){populateInfo(instructions)}),
);
}
You can use runtimeType, Use runtimeType to get the runtime type.
A property of the Object class, which is the base class of all objects in Dart, and of type Type.
for (final element in gameRef.children) {
if (element.runtimeType == Instructions) {
//my element exist in the scene
}
}
I used the last code for explain the use of runtimeType, but with Dart you have more options like to
children.query<Intructions>();

How to access control from the popup fragment by ID

I want my text area to be empty after I press OK button.
I have try this line this.byId("id").setValue("")
onWorkInProgress: function (oEvent) {
if (!this._oOnWorkInProgressDialog) {
this._oOnWorkInProgressDialog = sap.ui.xmlfragment("WIPworklist", "com.sap.FinalAssestments.view.WorkInProgress", this);
//this.byId("WIP").value = "";
//this.byId("WIP").setValue();
this.getView().addDependent(this._oOnWorkInProgressDialog);
}
var bindingPath = oEvent.getSource().getBindingContext().getPath();
this._oOnWorkInProgressDialog.bindElement(bindingPath);
this._oOnWorkInProgressDialog.open();
},
//function when cancel button inside the fragments is triggered
onCancelApproval: function() {
this._oOnWorkInProgressDialog.close();
},
//function when approval button inside the fragments is triggered
onWIPApproval: function() {
this._oOnWorkInProgressDialog.close();
var message = this.getView().getModel("i18n").getResourceBundle().getText("wipSuccess");
MessageToast.show(message);
},
The text area will be in popup in the fragment. I am expecting the text area to be empty.
If you instantiate your fragment like this:
sap.ui.xmlfragment("WIPworklist", "com.sap.FinalAssestments.view.WorkInProgress", this);
You can access its controls like this:
Fragment.byId("WIPworklist", "WIP").setValue(""); // Fragment required from "sap/ui/core/Fragment"
Source: How to Access Elements from XML Fragment by ID
The better approach would be to use a view model. The model should have a property textAreaValue or something like that.
Then bind that property to your TextArea (<TextArea value="{view>/textAreaValue}" />). If you change the value using code (e.g. this.getView().getModel("view").setProperty("/textAreaValue", "")), it will automatically show the new value in your popup.
And it works both ways: if a user changes the text, it will be automatically updated in the view model, so you can access the new value using this.getView().getModel("view").getProperty("/textAreaValue");.
You almost have it, I think. Just put the
this.byId("WIP").setValue("") line after the if() block. Since you are adding the fragment as a dependent of your view, this.byId("WIP") will find the control with id "WIP" every time you open the WIP fragment and set its value to blank.
You are likely not achieving it now because A. it is not yet a dependent of your view and B. it is only getting fired on the first go-around.

Find out if a leaflet control has already been added to the map

I wrote a custom Leaflet control. It's some kind of legend that may be added for each layer. The control itself has a close button to remove it from the map (like a popup).
The control can be added by clicking a button.
My problem is that the user may add the same control to the map several times. So what I need is to test if this specific control has already been added to the map and, if so, don't add it again.
I create a control for each layer, passing some options
var control = L.control.customControl(mylayer);
and add it to my map on button click
control.addTo(map);
Now imagine the control has a close button and may be closed. Now if the user clicks the button again, I only want to add the control if it's not already on the map - something like this (hasControl is pseudocode, there is afaik no such function)
if(!(map.hasControl(control))) {
control.addTo(map);
}
For simplicity I made an example where I create a zoom control and add it twice here.
Easiest way is to check for the existence of the _map property on your control instance:
var customControl = new L.Control.Custom();
console.log(customControl._map); // undefined
map.addControl(customControl);
console.log(customControl._map); // returns map instance
But please keep in mind, when using the _map property, that the _ prefix of the property implies that it's a private property, which you are normally not supposed to use. It could be changed or removed in future versions of Leaflet. You're not going to encounter that if you use the follow approach:
Attaching a reference of your custom control to your L.Map instance:
L.Control.Custom = L.Control.extend({
options: {
position: 'bottomleft'
},
onAdd: function (map) {
// Add reference to map
map.customControl = this;
return L.DomUtil.create('div', 'my-custom-control');
},
onRemove: function (map) {
// Remove reference from map
delete map.customControl;
}
});
Now you can check for the reference on your map instance like so:
if (map.customControl) { ... }
Or create a method and include it in L.Map:
L.Map.include({
hasCustomControl: function () {
return (this.customControl) ? true : false;
}
});
That would work like this:
var customControl = new L.Control.Custom();
map.addControl(customControl);
map.hasCustomControl(); // returns true
map.removeControl(customControl);
map.hasCustomControl(); // returns false
Here's a demo of the concept on Plunker: http://plnkr.co/edit/nH8pZzkB1TzuTk1rnrF0?p=preview

Widget changing hangs the main window in GTK+

I am writing a program in Vala using GTK+. It has a function that creates a ListBox that contains a lot of EventBox objects. There is one issue: there is one function that downloads the image and it takes a lot of time, so the main window didn't show up unless all downloads are finished. This is not what I wanted, I wanted main window to appear and then images to download and to be shown. So I separated image load to separate function, but main window still doesn't show unless all downloads are finished. What am I doing wrong?
Here is the function I'm using:
foreach (MediaInfo post in feedPosts)
feedList.prepend(post);
foreach (PostBox box in feedList.boxes)
box.loadImage();
("feedList" is a class inherited from Gtk.ListBox and "boxes" is a list containing all of PostBox (which is inherited from Gtk.EventBox) objects)
This is feedList.prepend function:
public void append(MediaInfo post)
{
Gtk.Separator separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
base.prepend(separator);
PostBox box = new PostBox(post);
base.prepend(box);
boxes.append(box);
}
And this is the constructor and loadImage functions of PostBox class:
public PostBox(MediaInfo post)
{
box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
this.add(box);
this.post = post;
userToolbar = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
userNameLabel = new Gtk.Label("#" + post.postedUser.username);
this.userNameLabel.set_markup(
"<span underline='none' font_weight='bold' size='large'>" +
post.postedUser.username + "</span>"
);
userToolbar.add(userNameLabel);
box.pack_start(userToolbar, false, true);
image = new Gtk.Image();
box.add(image);
box.add(new Gtk.Label(post.title));
box.add(new Gtk.Label( post.likesCount.to_string() + " likes."));
print("finished.\n");
return;
}
public void loadImage()
{
var imageFileName = PhotoStream.App.CACHE_URL + getFileName(post.image.url);
downloadFile(post.image.url, imageFileName);
Pixbuf imagePixbuf = new Pixbuf.from_file(imageFileName);
imagePixbuf = imagePixbuf.scale_simple(IMAGE_SIZE, IMAGE_SIZE, Gdk.InterpType.BILINEAR);
image.set_from_pixbuf(imagePixbuf);
}
You have written the download operations in another method, however the operations are still synchronous, i.e. they block the thread. You never want to do computationaly or otherwise expensive things in the GUI thread, because that makes the GUI unresponsive.
You should start your downloads asynchronously, and trigger a callback method when the download is complete. In the callback, then you may for example change the image placeholders to actual images.
I replaced all my async methods with multithreading and now it is working the way I want it to work.

Different Style object initialization with scala

When I using java there are something like double bracket initialization that actually make some runtime trade off. In scala I discover the simple way to initiate object property like
val button1: Button = new Button
button1.setText("START")
button1.setPrefWidth(100)
Which can be rewriten to
val button2: Button = new Button {
setText("PAUSE")
setPrefWidth(100)
}
Is these make any difference from performance or something else?
The difference is that in the first case you instantiate a new Button object and set its properties to some values (text = "START" and width = 100) and in the second case you create an anonymous class that inherits from Button and initialize its properties in its anonymous initializer (or constructor, not sure - Java's anonymous classes cannot have constructors).
The second case can be roughly rewritten like this (if it weren't an anonymous class):
class MyButton extends Button {
//constructor
setText("START")
setPrefWidth(100)
}
And when you call new MyButton you get an instance of MyButton with text set as "START" and prefWidth set as 100.
If you come from Java background consider this analogy:
Button button = new Button() {
//anonymous initializer
{
setText("START");
setPrefWidth(100);
}
};