Do you know when tap on an object in gameplay, to send me to a specific URL?
Here's the code:
function init(){
}
function start(){
}
function update(dt){
}
function signal(name, value, sender){
if(name=="touch" && value)
{
log("Touch Confirmed");
// HERE, I NEED TO ADD A TRIGGER THAT WOULD TAKE ME TO A WEBSITE.
}
}
function requested(name, arg, sender){
return this;
}
What code I should be writing under the function signal(name, value, sender){} to trigger a link, when that object is tapped or pressed by the user.
Thanks
Related
On the "Thinking in Compose" page I don't get this code, how does $clicks keep track of number of clicks?
#Composable
fun ClickCounter(clicks: Int, onClick: () -> Unit) {
Button(onClick = onClick) {
Text("I've been clicked $clicks times")
}
}
I'm learning Kotlin at the same time as Compose, so I get puzzled all the time.
It's omitted in that example but it should store the click-count in a MutableState<T> wrapped with remember
var clickCount by remember { mutableStateOf(0)}
ClickCounter(clicks = clickCount, onClick = {clickCount += it})
For real real real beginner like me, let me add my comment and code.
When the ClickCounter composable is called we need to pass two parameters.
To 'clicks', we pass initial value 0 which will be changed everytime we click the button. Also the initial value need to be 'remembered' and tracked for further changes. So we create new variable using 'mutableStateOf' function inside 'remember' function.
To 'onClick', we need to pass function which adds 1 everytime the button is clicked.
As a caller of the ClickCounter composable, I made a simple preview composable as below.
#Preview(showBackground = true)
#Composable
fun ClickCounterPreview(){
var clicks by remember { mutableStateOf(0) }
// We call ClickCounter composable passing two parameters.
ClickCounter(clicks = clicks, onClick = { clicks += 1 })
}
When a user clicks on a link, it uses routing to send the user to another component, you start at home and can click to charts. My problem is that I need to get the queryselector for the charts element, but when code is loaded from the component, it doesn't exist yet. How can I wait for the element to be created to execute the code?
I solved this by adding a MutationObserver inside the constructor.
Element chartsElement;
ChartsComponent() {
MutationObserver observer = MutationObserver(_onMutation);
Element my_app = querySelector('my-app');
observer.observe(my_app, childList: true);
}
_onMutation(List<dynamic> mutations, MutationObserver observer) {
mutations.forEach((value) {
MutationRecord record = value as MutationRecord;
if (record.addedNodes.contains('charts')) {
chartsElement = record.addedNodes[0];
}
});
observer.disconnect();
//Do stuff
}
I couldn't find an implementation of a double tap for Appium that was straightforward and allowed you to pass in the element locator strategy, so here goes:
public static void doubleTapElementBy(By by) {
WebElement el = getDriver().findElement(by);
MultiTouchAction multiTouch = new MultiTouchAction(getDriver());
TouchAction action0 = new TouchAction(getDriver()).tap(el).waitAction(50).tap(el);
try {
multiTouch.add(action0).perform();
} catch (WebDriverException e) {
logger.info("Unable to do second tap on element, probably because element requieres single tap on this Android version");
}
}
You can also try below approach using tap method in TouchAction class.
TouchAction taction = new TouchAction(driver);
taction.tap(tapOptions().withElement(ElementOption.element(YOUR_WebElement))
.withTapsCount(2)).perform();
You will need to add below static import as well:
import static io.appium.java_client.touch.TapOptions.tapOptions;
This is a workaround in pseudocode and possibly there's a more "official" way to do it, but it should do the work if no other solution is available:
Interpretmessages(){
switch(msg)
{
OnClick:
{ if (lastClicked - thisTime() < 0.2) //if it was clicked very recently
{doubleTapped()} //handle it as a double tap
else{lastClicked = thisTime()} //otherwise keep the time of the tap
} //end of OnClick
} //End of Message Handler
}//End of switch
}//End of messageHandler
If you have access to ready timer functions, you can set a function to be executed 0.2s after the click has gone off:
OnClick: if (!functionWaiting) // has the timer not been set?
{
enableTimer(); // set a function to go off in x time
clicks = 0; //we'll tell it that there's been one click in a couple of lines
} //set it for the first click
clicks++; //if it's already clicked, it'll become 2 (double tap) otherwise it's just one
So, the idea is that when you get a tap, you check if there's been another one recently (a. by checking the relative times, b. by checking if the function is still pending) and you handle it dependingly, only note that you will have to implement a timer so your function fires a bit later so you have time to get a second tap
The style draws upon the Win32's message handling, I'm pretty sure it works there, it should work for you too.
Double tap and hold -- Use below code:
new TouchAction(driver).press(112,567).release().perform().press(112,567).perform();
Double tap -- Use below code:
new TouchAction(driver).press(112,567).release().perform().press(112,567).release().perform();
Imagine, you want to listen to all clicks made to any anchor element on page. The anchors on page can be dynamically added/removed during the page lifetime and you want to register all of the click events, even on newly added.
Is there any way how to attach delegated event (like in jQuery) in Dart using its standard libraries?
In jQuery you can achieve this with element.on('click', '.selector', handler);.
You can now do that with ElementStream.matches like this :
document.body.onClick.matches('.selector').listen((Event event) {
print('Super cool!');
// The currently registered target for the event
// document.body
event.currentTarget;
// The element whose CSS selector matched
// .selector
event.matchingTarget;
// The target to which the event was originally dispatched
// the real element clicked under .selector
event.target;
});
Because I have found no viable solution, I have created package that enables delegation.
You can find it at
http://pub.dartlang.org/packages/delegate
Code is simple:
delegate(parent, condition, handler) {
return (event) {
var element = event.target;
while (element != parent && element != null) {
if (condition(element)) {
handler(event, element);
}
element = element.parent;
}
};
}
delegateOn(parent, String eventType, condition, handler) {
parent.on[eventType].listen(delegate(parent, condition, handler));
}
In a mini flash game, I have a few different level select buttons, and they all attach to one "levelChange()" function, and I'm just wondering if there is an attribute that stores which button was pressed or how to determine which was pressed if not.
Thanks
try the currentTarget property of MouseEvent example:
function buttonClick(event:MouseEvent):void
{
trace(event.currentTarget);
}
I recommend you store your buttons in variables outside your function like so:
var levelOne:MovieClip = levelOne;
So you can later call upon them like this:
function buttonClick(event:MouseEvent):void
{
if (event.currentTarget == levelOne) {
trace("level one selected");
else if (event.currentTarget == levelTwo) {
trace("level two selected");
}
}
If you're using AS3 you can add a switch statement in your levelChange() function.
For example.... if you have 2 buttons, one with the instance name of "level1", and the other "level2".
function levelChange( me:MouseEvent ):void
{
switch( me.currentTarget.name )
{
case "level1":
// Go to Level 1 here.
case "level2":
// Go to Level 2 here.
}
}