I have a Letter class like this:
class Letter : Label {
val char: Char
var interactable = true
constructor(char: Char) : super(""+char, H.letterStyle()) {
this.char = char
}
fun animateSelect() {
addAction(Actions.scaleTo(3.0f, 3.0f, 0.5f))
}
fun animateUnselect() {
addAction(Actions.scaleTo(3.0f, 3.0f, 0.5f))
}
}
In my touch listener, I have this:
override fun touchDown(event: InputEvent?, x: Float, y: Float, pointer: Int, button: Int): Boolean {
var currentInteractingLetter: Letter? = null
for (letter in letterList) {
if (letter.bound.contains(x, y)) {
currentInteractingLetter = letter
break
}
}
if (currentInteractingLetter == null) {
} else {
selectedLetters.add(currentInteractingLetter)
currentInteractingLetter.animateSelect()
currentInteractingLetter.interactable = false
}
return true
}
The logic is quite straightforward. When user touch any one of the letters, I will invoke animateSelect() function.
When I run it, animateSelect did get called, but there is no scaleUp effect. I have tried to clear all actions before addAction but still the same.
Labels don't directly support scaling.
The easy way to solve this is put the label in a Container, setTransform(true) on the Container, and add your scale action to the Container.
val container= Container<Label>().apply {
isTransform=true
actor=label // Set your Label to container
}
container.addAction(Actions.scaleTo(3.0f, 3.0f, 0.5f))
Related
I am using ngx-formly v: 5.5.10. I try to check if field is focused in the expressionProperties.
This is necessary for changing the value based on focus. Something like this:
expressionProperties: {
'model.testField': (m) => {
if (m.testField.value && testField.focus=true) {
return x;
} else {
return y;
}
}
}
Is there a formly built-in solution for checking focus in the expressionProperties?
Thanks for any help!
The field instance is passed as a third argument of the expression callback:
expressionProperties: {
'model.testField': (m, formState, field) => {
if (m.testField.value && field.focus === true) {
return x;
} else {
return y;
}
}
}
I'm trying to implement classes DistanceCM and DistanceMM, and I want these to be summable interchangeably, as long as they both inherit from Distance.
However, I get this error:
"Error:(46, 76) ceylon: type parameter 'Other' of declaration
'Summable' has argument 'Distance' which is not assignable to upper
bound 'Summable' of 'Other'"
...which I can't decipher... The error message refers to this line in the code below:
shared actual Distance plus(Distance other)
This is the current code:
abstract class Distance() of DistanceMM | DistanceCM {
shared formal Distance multiplyScalar(Float scalar);
}
class DistanceMM(variable Float val) extends Distance() satisfies Summable<Distance>
{
shared Float distanceInMillimeters;
shared Float distanceInCentimeters;
switch (unit)
case (millimeter) {
distanceInMillimeters => val;
distanceInCentimeters => val / 10;
}
case (centimeter) {
distanceInMillimeters => val * 10;
distanceInCentimeters => val;
}
shared actual DistanceMM multiplyScalar(Float scalar) {
val = val * scalar;
return this;
}
shared actual Distance plus(Distance other) {
switch (other)
case (DistanceMM) {
return DistanceMM(val + other.distanceInMillimeters(), unit);
}
case (DistanceCM) {
return DistanceMM(val + other.distanceInCentimeters(), unit);
}
}
}
class DistanceCM(variable Float val) extends Distance() satisfies Summable<Distance>
{
shared Float distanceInMillimeters;
shared Float distanceInCentimeters;
switch (unit)
case (millimeter) {
distanceInMillimeters => val;
distanceInCentimeters => val / 10;
}
case (centimeter) {
distanceInMillimeters => val * 10;
distanceInCentimeters => val;
}
shared actual DistanceCM multiplyScalar(Float scalar) {
val = val * scalar;
return this;
}
// implementation missing
}
interface Summable<Other> of Other​ given Other satisfies Summable<Other>
Notice the constraint (the given clause). You're claiming that DistanceMM satisfies Summable<Distance>, but Distance doesn't satisfy the constraint on Other (Distance doesn't satisfy Summable<Distance>). Try this:
interface Distance of Centimeter | Millimeter satisfies Summable<Distance> {}
class Centimeter() satisfies Distance {
shared actual Distance plus(Distance other) => nothing;
}
class Millimeter() satisfies Distance {
shared actual Distance plus(Distance other) => nothing;
}
I'm sorry if this has been asked before, I can't find an answer. I would like to create custom parameter types for a function.
Typedef?/Define type
direction
{
LeftToRight,
RightToLeft
};
Function:
class func animateIn (dir:direction)
{
if dir = LeftToRight
{
// animate left to right
}
else
{
// animate right to left
}
}
Call:
animateIn (dir:LeftToRight)
enum seems the perfect candidate for this use. If you plan to have more cases in the enum, a switch statement also seems to be more feasible inside the function.
enum Direction {
case leftToRight, rightToLeft
}
class func animateIn(dir: Direction){
switch dir{
case .leftToRight:
//do something
case .rightToLeft:
//do something
}
}
enum Direction
{
case leftToRight, rightToLeft
}
Function:
class func animateIn(dir:Direction)
{
switch dir {
case .leftToRight:
// animate left to right
default:
// animate right to left
}
}
Call:
animateIn(dir:.leftToRight)
So i have an interactor which performs an insert operation with Realm and then notifies that the insertion is completed with a RealChangeListener. It is something like this:
fun insertCar(item: Car) {
realm.doInTransaction {
val car = Car(...)
val copy = copyToRealm(car)
copy.addChangeListener(...)
}
}
I can do it this way:
fun insertCar(item: Car, listener: RealmChangeListener<Car>) {
realm.doInTransaction {
val car = Car(...)
val copy = copyToRealm(car)
copy.addChangeListener(listener)
}
}
And access like this:
realmInteractor.insertCar(item, RealmChangeListener {
// do something here
})
But then i have no way of removing this listener
realmInteractor.insertCar(item, RealmChangeListener {
// do something here
it.removeChangeListener(this)
})
this will point to the class its located not the actual listener
I can also do this:
fun insertCar(item: Car, doAfterChange (Car) -> Unit) {
realm.doInTransaction {
val car = Car(...)
val copy = copyToRealm(car)
copy.addChangeListener(RealmChangeListener{
doAfterChange()
})
}
}
But then i have a SAM inside another SAM (too overkill imo)
I can do it like this:
fun insertCar(item: Car, listener: RealmChangeListener<Car>) {
realm.doInTransaction {
val car = Car(...)
val copy = copyToRealm(car)
copy.addChangeListener(listener)
}
}
realmInteractor.insertCar(item, object : RealmChangeListener<Car> {
override fun onChange(element: Car?) {
...
element?.removeChangeListener(this)
}
})
Which works but it is too verbose.
So how do you deal with this and what is considered the best approach?
You can create a generic method to perform a run-once listener. Something along the lines of:
fun <T> createInRealm(objectFactory: () -> T, changeListener: (T) -> Unit) {
realm.doInTransaction {
val obj = objectFactory()
val copy = copyToRealm(obj)
copy.addChangeListener(object : RealmChangeListener<T> {
override fun onChange(element: T) {
changeListener()
element.removeChangeListener(this)
}
}
}
}
I have a class file that has some netstreams and connections and I'm trying to remove the class completely. Here's my code:
function onTestProcessed(e:CustomEvent2):void {
// Remove
rtmp_test.removeEventListener(CustomEvent2.PASS_PARAMS, onTestProcessed);
e.currentTarget.parent.removeChild(e.currentTarget);
// Validation comparison
if (e.boo == false) {
trace("event.boo = ", e.boo);
} else {
trace("event.boo = ", e.boo);
}
}
This code removes the display of video but not its sound. Do I have to find each variable in the class and remove them one by one?
Seems like this solved the problem:
function onTestProcessed(e:CustomEvent2):void {
// Remove
rtmp_test.removeEventListener(CustomEvent2.PASS_PARAMS, onTestProcessed);
rtmp_test.netStreamObj.close();
// rtmp_test.netStreamObj = null;
rtmp_test.vid.clear();
e.currentTarget.parent.removeChild(e.currentTarget);
// Validation comparison
if (e.boo == false) {
trace("event.boo = ", e.boo);
} else {
trace("event.boo = ", e.boo);
}
}
as3 - How to stop video and detach NetStream