Rescheduling same method in Cocos2d-x 3.1 - cocos2d-x

I have the following code in Cocos2d-x v3.1:
void Board::createNewRandomBottomRow()
{
//eventHappening is a variable modified in other parts of the code
//I don't want that while eventHappening is true, this method does anything
if ( eventHappening ) {
this -> scheduleOnce( SEL_SCHEDULE( &Board::createNewRandomBottomRow ), 0.3f );
}
else
{
//actual logic
}
}
However, I check with the debugger that the method is not being rescheduled. Isn't it possible to reschedule a method from inside the same method?

Well, I did the same again in another simpler scenario, and the rescheduling works fine, so I guess I messed up in the logic in another part of the code.
So just to sum up: rescheduling a method from inside the method itself works fine.

Related

How do I create the onClick function for a button with Kotlinx.html?

I am using Kotlin's html library kotlinx.html for dynamic html building.
I want to create a button which triggers a function when clicked. This is my current code:
class TempStackOverflow(): Template<FlowContent> {
var counter: Int = 1
override fun FlowContent.apply() {
div {
button(type = ButtonType.button) {
onClick = "${clicked()}"
}
}
}
fun clicked() {
counter++
}
}
This results in the following source code:
<button type="button" onclick="kotlin.Unit">testkotlin.Unit</button>
Which gives this error when clicked (from Chrome developer console):
Uncaught ReferenceError: kotlin is not defined at HTMLButtonElement.onclick
I have tried several approves, and search for a solution - but could not find the proper documentation.
I am not a Kotlin expert, but it's perfectly possible to write event handlers using kotlinx. Rather than:
onClick = "${clicked()}"
have you tried using this?
onClickFunction = { clicked() }
If you really need a bit on Javascript here, you can type this:
unsafe {
+"<button onClick = console.log('!')>Test</button>"
}
Good for debugging and tests, but not very nice for production code.
Unfortunately, you're completely missing the point of the kotlinx.html library. It can only render HTML for you, it's not supposed to be dynamic kotlin->js bridge, like Vaadin or GWT. So, you just set result of clicked function converted to String to onClick button's property, which is effective kotlin.Unit, because kotlin.Unit is default return value of a function if you not specify another type directly.
fun clicked() {
counter++
}
is the same as
fun clicked():Unit {
counter++
}
and same as
fun clicked():Kotlin.Unit {
counter++
}
So, when you set "${clicked()}" to some property it actually exec function (your counter is incremented here) and return Koltin.Unit value, which is becomes "Kotlin.Unit" string when it rendered inside "${}" template

How to write custom sort logic on sort column event in ng2-smart-table

I'm looking to hook-up sort events performed on ng2-smart-table. Followed https://akveo.github.io/ng2-smart-table/#/documentation, I see bunch of events that are exposed like rowSelect, mouseover etc but I don't see sort events published/emitted by the library. I'm thinking of changing Ng2SmartTableComponent and emit an event when (sort) is called internally. May I know if anyone did it already or is there a hack I can rely upon.
The source of the sort in ng2-smart-table is shown on GitHub (link to code).
If you want to change the compare-Function (as used by default) you can add your own custom function in your ng2-smart-table-configuration:
columns: {
group_name: {
title: 'Groupname',
compareFunction(direction: any, a: any, b: any) => {
//your code
}
}
}
I was searching for an event to sort my data remotely and I have found a solution. Also I have some logic for page change event (remote paging). Here is what works for me.
ts
source: LocalDataSource = new LocalDataSource();
ngOnInit() {
this.source.onChanged().subscribe((change) => {
if (change.action === 'sort') {
this.sortingChange(change.sort);
}
else if (change.action === 'page') {
this.pageChange(change.paging.page);
}
});
}
html
<ng2-smart-table [settings]="settings" [source]="source"></ng2-smart-table>
This solution won't replace custom logic but it might help you solve your problem.

React upgrade: "this" visibility in getDefaultProps

I am upgrading some older react component I inherited (v0.10.0) to work with the latest version of react (v0.14.8).
The following scenario stopped working:
// within a react component
onClick: function() {
// DO SOMETHING
}
getDefaultProps: function () {
return {
someProp: 'prop',
onClick: this.onClick
}
}
This is easily resolved moving the code into an anonymous function, like the following:
getDefaultProps: function () {
return {
someProp: 'prop',
onClick: function() {
//DO SOMETHING
}
}
}
My question is: why has the visibility of 'this' changed at that level and what's the best way to refactor this code? And what if I had-to/wanted-to use 'this' at that level?
Any help appreciated, as a disclaimer I am a react super-beginner!
The result of getDefaultProps() is shared across all instances of a component. That means that the result can't rely on any particular instance of the component. The reason it changed is likely because of the performance benefit from caching, although I can't say for sure.
As for refactoring the code, I'm not sure there's a silver-bullet here. From my perspective what you currently have seems like an anti-pattern. Props are meant to be passed in by consumers that have no knowledge of the inner workings of the component, so it seems odd that a default value for a prop would depend on the inner workings. Without knowing exactly what you're doing, I would say your best bet is to just use null as the default value for the prop, then check the value at runtime when you do have access to the this context.
handleSomeAction() {
if (!this.props.onClick) {
// DO SOMETHING
}
}

Grails 2.0.4 webflow "type mismatch" exception

I'm still pretty new to Grails and I'm developing an online survey. I decided to use web flow and I have been running into many issues. I'm trying to pass the survey id from the gsp page to the flow controller. This works perfectly fine on any other controller action but whenever I do it to the action for the start state of the flow I always get the same error. I've followed a tutorial in a text book that does this the EXACT same way and I'm running out of ideas.
here is the link from the gsp page:
<g:link controller="surveyPage" action="beginTest" id="${survey.id}">
${survey.surveyName}
</g:link>
and here is the flow with the start state
def beginTestFlow = {
showSurvey{
def survey = Survey.get(params.id)
on("cancel").to "cancelSurvey"
on("continueSurvey").to "nextQuestion"
}
cancelSurvey { redirect(controller:"surveyPage") }
}
it always throws the exception:
argument type mismatch on the line with
def survey = Survey.get(params.id)
I've also tried:
flow.survey = Survey.get(params.id)
or even:
flow.survey = Survey.get(session.survey.id)
always the same error. Also, I made sure class Survey implements Serializable. I've copied and pasted the same code into a different action with the same controller and it works flawlessly. Any ideas to what is different with the web flow?
You can't put code like that directly inside a state definition, you need to use an action state or an onEntry block
def beginTestFlow = {
showSurvey{
onEntry {
flow.survey = Survey.get(params.id)
}
on("cancel").to "cancelSurvey"
on("continueSurvey").to "nextQuestion"
}
cancelSurvey { redirect(controller:"surveyPage") }
}
The onEntry block will fire every time the showSurvey state is entered. If instead you want some logic to be run just once at the start of the whole flow (for example if some later transition might re-enter the initial state), you can use a flow-level onStart block instead:
def beginTestFlow = {
onStart {
flow.survey = Survey.get(params.id)
}
showSurvey{
on("cancel").to "cancelSurvey"
// ...
Ivo Houbrechts wrote an excelent tutorial about grails 2.0 webflow. You can read it here:
http://livesnippets.cloudfoundry.com/docs/guide/

try statement tripping debugger (as3)

Trying a try-catch sequence that works fine in release version, but running it in debugger causes errors to be displayed. Obviously there errors, that's why I'm using this stuff inside try, but I'm wondering if there's any way I can get debugger to stop stopping. While I don't even mind the error message, the app no longer executes properly.
I've got a this[$val] that I needs to return a null if there is no such variable inside the class.
try {
return this[$val]+"";
} catch(error:ArgumentError) {
// Do nothing
}
return "";
again, this works like it is supposed to, but it causes errors in debugger
any ideas for an alternative?
I think you are catchin an argument error in place of the real problem of handling a null object + string error.Try Using;:
try {
return this[$val]+"";
} catch(error:Error) {
// Do nothing
}
return "";