I'm trying to use flash8 to create an drag and drop event using the mouse.
My code is:
import flash.events.MouseEvent;
circle_mc.addEventListener(MouseEvent.MOUSE_DOWN,downf);
circle_mc.addEventListener(MouseEvent.MOUSE_UP,upf);
function downf(e:MouseEvent) { circle_mc.startDrag(); }
function upf(e:MouseEvent) { circle_mc.stopDrag(); }
I get these errors:
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: Statement must appear within on/onClipEvent handler
circle_mc.addEventListener(MouseEvent.MOUSE_DOWN,downf);
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Statement must appear within on/onClipEvent handler
circle_mc.addEventListener(MouseEvent.MOUSE_UP,upf);
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: The class or interface 'MouseEvent' could not be loaded.
function downf(e:MouseEvent) {
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: The class or interface 'MouseEvent' could not be loaded.
function upf(e:MouseEvent) { circle_mc.stopDrag(); }
Total ActionScript Errors: 4 Reported Errors: 4
I don't understand why this is happening. On Internet I've found that this error may be caused due to the version of AS3 or AS2, but I also can't find the version I use.
Any help is appreciated.
The code you've posted is clearly AS3 but since you're using Flash 8 you're limited to AS2. No problem though, you can achieve the same using AS2 of course.
Draw a circle using the Oval Tool and select it using the Selection Tool
Hit F8 to convert it to a symbol and hit OK
In the Properties panel give the instance a name e.g. circle_mc
Open the Actions window (F9) and select Scene 1 -> Layer 1 : Frame 1
Paste the following code
circle_mc.onPress = function()
{
startDrag(this, true);
}
circle_mc.onRelease = function()
{
this.stopDrag();
}
Related
I am playing around with Vala and GTK4.
FileChooserDialog is not working for me
using Gtk;
int main (string[] argv) {
// Create a new application
var app = new Gtk.Application ("com.example.GtkApplication",
GLib.ApplicationFlags.FLAGS_NONE);
app.activate.connect (() => {
// Create a new window
var window = new Gtk.ApplicationWindow (app);
window.title = "File chooser";
window.set_default_size (350, 70);
window.resizable = false;
// Create a new button
var file_choose_button = new Gtk.Button.with_label ("...");
file_choose_button.clicked.connect (() => {
var fileChooser = new FileChooserDialog(
"Select File",
window,
FileChooserAction.OPEN,
"Cancel",
ResponseType.CANCEL,
"Open",
ResponseType.ACCEPT,
null);
fileChooser.response.connect(()=> {
stdout.printf("File selectd!");
});
// WHAT TO DO IN ORDER TO SHOW FILE CHOOSER?
});
window.set_child (file_choose_button);
// Show
window.present ();
});
return app.run (argv);
}
I am missing some important piece of code, that will cause the FileChooserDialog to "appear".
In previous Versions of GTK there is "dialog.run" - which is missing in GTK4.
The C-Example on https://docs.gtk.org/gtk4/class.FileChooserDialog.html uses makro(?) "gtk_widget_show(xxx)" for which I was not able to find an representation in Vala.
Any help appreciated!
Best Regards
Emil
After some struggle the solution was found (and is pretty simple).
As stated in the Vala Documentaion Site - File Chooser Dialog
It inherits from couple of classes one of which is GTK.Window.
So it is as simple as calling the present() method.
Thus the missing command above is:
fileChooser.present();
One should not forget to use the close() method once file was selected or selection was canceled.
Important note:
"gtk_widget_show()" representation in Vala is GTK.Widget.show() BUT
I was not clever enough to find out how to prepare the parameter.
It expects pointer (GtkWidget*) and simply passing the "fileChooser" causes all kinds of compiler exceptions.
May be someone can throw more light on this (as I am using Vala to avoid the use of C - I am clearly not the expert in this area)
I defined an NS_OPTIONS in Objective-C .h file:
typedef NS_OPTIONS (NSInteger, Options){
OptionsOne,
OptionsTwo,
OptionsThree
};
Now when accessing from Swift:
public func myFunc() -> Options {
return [.one, .two]
}
I am getting this error:
'one' is unavailable: use [] to construct an empty option set.
But I am not getting this error for .two or .three. It appears only for the first option.
By default, in Swift 3, an NS_OPTIONS enumerand equating to 0 is not imported into Swift by name. You have to use [] in Swift to get it.
When you changed the enumerand's value to 1, the name was imported.
If you think about it, this makes perfect sense. NS_OPTIONS is for bitmasks. Thus, if (let's say) .one is 0 and .two is 1, there is no useful meaning to the expression [.one, .two] because there is no information added by the presence of the .one.
What you were doing, on the other hand, was always a misuse of NS_OPTIONS, since it was not a bitmask. Your modification turned it into one. (Objective-C does not magically generate bitmask-appropriate values for you.)
I have found the solution to this to be adding explicit bitmask value to the options:
typedef NS_OPTIONS (NSInteger, Options){
OptionsOne = 1 << 0,
OptionsTwo = 1 << 1,
OptionsThree = = 1 << 2
};
and the error went away.
I have some problems with motions and tweens:
I want move my sprite to to the bottom right corner (800,600) then to the top left corner (0,0). But my tweens arent waiting each other.
motion.toBotCorner(currentSprite);
motion.toTopCorner(currentSprite);
And this is in my Motion class:
public function toBotCorner(currSpr:Sprite):void {
TweenLite.to(currSpr, 3, {x:800, y:600});
}
public function toTopCorner(currSpr:Sprite):void {
TweenLite.to(currSpr, 3, {x:0, y:0});
}
How to make the first one proccess and then the second? Thank you!
You should use the 'onComplete' provided by TweenLite on your first animation. It requires a method name, and use the 'onCompleteParams' to send the parameters to the method call.
So, your code would look like this now:
public function toBotCorner(currSpr:Sprite):void {
TweenLite.to(currSpr, 3, {x:800, y:600, onComplete:toTopCorner, onCompleteParams:[currSpr]});
}
public function toTopCorner(currSpr:Sprite):void {
TweenLite.to(currSpr, 3, {x:0, y:0});
}
Note that the onCompleteParams: is an array as a method could have multiple parameters to be passed.
Here's what the docs says:
onComplete : Function - A function that should be called when the tween has completed
onCompleteParams : Array - An Array of parameters to pass the onComplete function.
Hope this helps. Please accept this answer if it works for you, that would close the question. Thanks!
Is there a way to increase execution speed of a playground?
I want to iterate many cycles and not to wait 10 minutes.
For example:
import UIKit
var count = 0
for var i = 0; i < 1000000000; i++ {
count++
}
This code will execute a way too long. But I want to get quick result.
One of the biggest performance killer is the output at the right side of the playground. Now I will show you how to minimize this output.
See at the end for your example code.
Best Performance
The most performant way is to make all the performance critical code in a .swift file inside the Sources folder in the playground.
Note: In order to use the functions, classes, properties and methods from the Sources folder you have to mark them public. If you want to subclass a class it has to be marked open.
Good Performance but ugly code
The following method (I think this is not official/intended) can be used to disable the playground output but also leads to ugly code. However it is good for temporary disabling the output.
There are two main ways (and two tricks) to achieve the minimum amount of output (If you find a better way let us know):
Use parenthesis around Void (or Void?) expressions like assignments (normally leads to no output, see also 3.).
var x = 0 // output: 0
(x = 1) // NO output
(x = 2 * x - 1) // NO output
(x.negate()) // NO output
Note: In Swift an assignment returns Void and in case of optional chaining it is Void?.
var x: (Int, Int)? = nil
if (x?.0 = 0) != nil {
// assignment was successful (x!=0 and now x=(0, x.1))
} else {
// assignment was not successful (x==nil)
}
Initialize and declare variables separately.
var x: Int // NO output
(x = 0) // NO output
If 1. does not work add an additional no-op (no operation) line above or below ().
This happens in single line closures (and probably in some other contexts) for example: (see also the code below)
[1, 4, 5, 6].mmap{
() // without this line the line below would yield to an output
($1 = $0 + 1)
} as [Int]
Instead of wrapping every line in parenthesis you can also use a tuple of all the expressions which is then assigned to a variable:
var a: Any // this may be a useful definition in this context
var x: Int
var y: Int
(a = (x = 0,
y = 1,
x = y + 1,
y = x*x))
However this could lead to a indentation disaster...
Where it does not work (I've found no way how to remove the output; This list is probably not complete):
returns in functions and closures
Declaration of Optional variables e.g.: var x: Int?
An example of a new map method on Sequence
Usage: See above at Point 3.
The signature of Sequence.map is
func map<T>(_ transform: (Self.Element) throws -> T) rethrows -> [T]
Since I have not found a way how to remove the output of returns one can use a closure with an inout argument (get the "return" value with an assignment). A possible signature could then be:
func mmap<U>(_ transform: (Element, inout U?) -> ()) -> [U]
so we can pass nil in the inout argument since it is a good default for every possible U without imposing a constraint on U which could require an instance generator (e.g.: init() { ... }).
Unfortunately Swfit has a hard time to infer U so you would need to help the compiler with explicit type annotations. In addition var newElement: U? does return nil in the sidebar.
Now I will use Any instead of U?:
extension Sequence {
// ATTENTION: this is not as performant as the normal `map`!
func mmap<U>(transform: (Element, inout Any) -> ()) -> [U] {
var result: [U]
(result = [U]())
for element in self {
var newElement: Any
(newElement = 0) // some placeholder element
(transform(element, &newElement))
// assume the inout element to be of type `U`
(result.append(newElement as! U))
}
return result // the ONLY output in this method
}
}
Your example code
Using Swift 4
var count = 0
for i in 0..<1_000_000_000 {
(count += 1)
if count % 100_000 == 0 {
// print only every 100_000th loop iteration
print(count)
}
}
Without the parenthesis: about 10.000 loop iterations per second
With parenthesis: about 10.000.000 loop iterations per second !!!
I feel your pain, I was playing around with printing 2D functions to [Double] then converting to UIImageView. One of the steps was iterating over millions of pixels, and it took forever.
Anything computationally intensive, or repetitive, or potentially time consuming should be put in the "Sources" folder of the playground. That way the code is precompiled before your playground is run. Put the output of that for loop in a public function that you can call from the playground. Then you won't have to sit there watching the playground count all the times it went through the for loop.
I had a such problem and I have solved it after days of trials. All I needed to do is move all my code in folder Sources of playground. So, after that the execution speed was enhanced.
I hope it helps you.
Note: don't forget to use open classes.
To speed up the build in Xcode Playground and prevent the loading icon to keep spinning forever:
go to the sidebar on the right, and change iOS to macOS in Playground Settings
instead of importing UIKit, import Foundation
(this would work if you're not trying to use specific stuff from the UIKit framework)
I have this code at viewmodel:
self.events = ko.observableArray();
function getAllEvents() {
dataservice.events.getAll()
.then(queryEventsSucceeded)
.fail(queryEventsFailed);
}
function queryEventsSucceeded(data) {
self.events(data.results);
}
function queryEventsFailed(error) {
logger.logError(error.message, "Error retrieving events");
}
Then I have a view like this:
<div data-bind="wijevcal: { appointments: events }"></div>
But this code throws Maximum call stack size exceeded exception.
When I fill self.events() with hard coded events like the following code, it works perfectly:
self.events([{
id: "event1",
subject: "Green event.",
start: new Date(2013, 4, 18, 9),
end: new Date(2013, 4, 18, 11),
allday: true,
description: "The green event.",
color: "green"
}]);
I can't figure out what is happening...
My guess is that the Wijmo component can't handle an object with circular references (e.g., Customer has Orders and each order has a Customer) as it walks the bound object graph. Even entity types without navigation properties have the entity.entityAspect.entity circular reference.
You'll have to find a way to break that cycle. You might contact the Wijmo folks to see what they recommend.
The immediate workaround is not that pretty: you'll have to map the events into an intermediate objects (each an "ItemViewModel") that exposes exactly the information you need for binding. This is extra work which is OK once in a while but not something you want to do routinely.