unterminated function-like macro invocation - cocos2d-x

bool HelloWorld::init()
{
bool bRet = false;
do {
CC_BREAK_IF(!CCLayerColor::initWithColor( ccc4(255,255,255,255));
} while (0);
}s
my cocos2d-x is the last versions, error at CC_BREAK_IF(!CCLayerColor::initWithColor( ccc4(255,255,255,255)); what can i do ? my ide is xcode6.2
in the older versions, code as above, i have a question: initWithColor is not static function why it can be called by CCLayerColor?

In HelloWorldScene.h
Change
class HelloWorld : public cocos2d::CCLayer
To
class HelloWorld : public cocos2d::CCLayerColor
Change this line from
CCSprite *bg1 = CCSprite::create("abc");
To
CCSprite *bg1 = CCSprite::create("abc.png");
Use do while loop(you have written "s" instead of while in the end)

Related

How can I solve 'Duplicate Constructor' error in Haxe?

In Haxe, I created a class named MyClass like:
class MyClass {
var score: String;
public function new (score: Int) {
this.score = Std.string(score);
}
public function new (score: String) {
this.score = score;
}
}
I need multiple constructors but Haxe does not allow me to do. It throws this error from building phase:
*.hx:*: lines * : Duplicate constructor
The terminal process terminated with exit code: 1
How can I solve this problem?
This is known as method overloading, which is not supported by Haxe apart from externs (but might be in the future). There's multiple ways you could work around this.
A common workaround in the case of constructors would be to have a static "factory method" for the second constructor:
class MyClass {
var score:String;
public function new(score:String) {
this.score = score;
}
public static function fromInt(score:Int):MyClass {
return new MyClass(Std.string(score));
}
}
You could also have a single constructor that accepts both kinds of arguments:
class MyClass {
var score:String;
public function new(score:haxe.extern.EitherType<String, Int>) {
// technically there's no need for an if-else in this particular case, since there's
// no harm in calling `Std.string()` on something that's already a string
if (Std.is(score, String)) {
this.score = score;
} else {
this.score = Std.string(score);
}
}
}
However, I wouldn't recommend this approach, haxe.extern.EitherType is essentially Dynamic under the hood, which is bad for type safety and performance. Also, EitherType is technically only intended to be used on externs.
A more type-safe, but also slightly more verbose option would be haxe.ds.Either<String, Int>. Here you'd have to explicitly call the enum constructors: new MyClass(Left("100")) / new MyClass(Right(100)), and then use pattern matching to extract the value.
An abstract type that supports implicit conversions from String and Int might also be an option:
class Test {
static function main() {
var s1:Score = "100";
var s2:Score = 100;
}
}
abstract Score(String) from String {
#:from static function fromInt(i:Int):Score {
return Std.string(i);
}
}
Finally, there's also an experimental library that adds overloading support with macros, but I'm not sure if it supports constructors.
I recommend to use type parameter
class MyClass<T> {
var score:String;
public function new(score:T) {
this.score = Std.string(score);
}
}
You can also use type parameter at constructor
class MyClass {
var score:String;
public function new<T>(score:T) {
this.score = Std.string(score);
}
}
However, T used at constructor fails at runtime (CS and Java), it is not fixed yet (Haxe 4). Otherwise, you could do this
class MyClass {
var score:String;
#:generic public function new<#:const T>(score:T) {
this.score = Std.is(T, String) ? untyped score : Std.string(score);
}
}
which nicely produce code like this (CS)
__hx_this.score = ( (( T is string )) ? (score) : (global::Std.#string(score)) );
causing Std.string() to be called only if T is not a String.
Hej,
With a simple example as it is, you can just do something like that function new( ?s : String, ?n : Int ){} and Haxe will use the correct argument by type. But you'll be able to do new() and maybe you don't want.

How to initialize c++/cx class with aggregate initialization?

I have this ref class:
namespace N
{
public ref class S sealed
{
public:
property Platform::String^ x;
};
}
How do I initialize it in place with the aggregate initializer?
I have tried:
N::S s1 = { %Platform::String(L"text") };
but the compiler says
error C2440: 'initializing': cannot convert from 'initializer list' to
'N::S'
Also:
N::S s1 { %Platform::String(L"text") };
and the error is:
error C2664: 'N::S::S(const N::S %)': cannot convert argument 1 from
'Platform::String ^' to 'const N::S %'
This works greatly with the standard c++ like this:
struct T
{
wstring x;
};
T x { L"test" };
I do not want to use a constructor here.
I assume you mean you don't want a public constructor on the projected WinRT type -- no problem, you can use the internal keyword to mean "public inside C++ but not exposed through interop". That means you can even use native C++ types for your parameters if you like:
namespace Testing
{
public ref class MyTest sealed
{
public:
property String^ Foo {
String^ get() { return m_foo; }
void set(String^ value) { m_foo = value; }
}
internal:
// Would not compile if it was public, since wchar_t* isn't valid
MyTest(const wchar_t* value) { m_foo = ref new String(value); }
private:
String^ m_foo;
};
}
MainPage::MainPage()
{
// Projected type does NOT have this constructor
Testing::MyTest t{ L"Hello" };
OutputDebugString(t.Foo->Data());
t.Foo = "\nChanged";
OutputDebugString(t.Foo->Data());
}
Also you don't need to have the private variable to hold the string -- you could just use the auto-property as in your original code -- but I prefer to be explicit. It also means that if you needed to access the string a lot from within your C++ code you could provide an internal accessor function and not have to go through a vtable call to get at it.

runAction not excuted when invoked from CCNode (not onEnter )

I have Class that is CCNode extended from one of its methods i want to excute actions
but none of then are running :
from this class :
class GameController : public CCNode
where i have also this:
void GameController::onEnter()
{
CCNode::onEnter();
}
this is the code i have :
bool GameController::removeFinalGems(CCArray* gemsToRemove)
{
onGemScaleInAnim = CCCallFuncND::create(this,
callfuncND_selector(GameController::OnGemScaleInAnim),gemsToRemove);
onRemoveGemScaleInAnim = CCCallFuncND::create(this,
callfuncND_selector(GameController::OnRemoveGemScaleInAnim),gemsToRemove);
CCSequence* selectedGemScaleInAndRemove = CCSequence::create(onGemScaleInAnim,
onRemoveGemScaleInAnim,
NULL);
bool b = this->isRunning();
CCAction *action = this->runAction(selectedGemScaleInAndRemove);
return true;
}
void GameController::OnGemScaleInAnim(CCNode* sender,void* data)
{
CCArray *gemsToRemove = (CCArray*) data;
}
void GameController::OnRemoveGemScaleInAnim(CCNode* sender,void* data)
{
CCArray *gemsToRemove = (CCArray*) data;
}
also i added check to see if there is actions running before and after
and its look like before its equal 0 and after it is equal 1
int na = this->numberOfRunningActions(); //equal 0
CCAction *action = this->runAction(selectedGemScaleInAndRemove);
int na0 = this->numberOfRunningActions();//equal 1 so there is action
it never gets to the OnRemoveGemScaleInAnim and OnGemScaleInAnim methods
I ran into this problem on cocos2d-x when porting an iOS cocos2d app.
After your "runAction" call, add the following line of code to check to see if paused target(s) is the culprit:
CCDirector::sharedDirector()->getActionManager()->resumeTarget( this );
In my case, this was the reason why actions were not being run. It seems that CCTransitionFade's (from screen transitions) resulted in pauses. It might be due to construction of nodes done during init, although I have not tested that theory.

How to call a static function on an ActionScript object's ancestor class?

The Ancestor class does, indeed, have a function called (for the sake of example) "foo".
public static function callAncestorStaticMethod() : void
{
var ancestorClassName : String = getQualifiedSuperclassName(Descendant);
var ancestorClass : Class = Class(getDefinitionByName(ancestorClassName));
ancestorClass.foo(); // <---- runtime error here: foo is not a function
}
Examining ancestorClass finds it an Object with no visible properties (ancestorClass.prototype does not either).
So, how do I call a static function on a class when I only have its name as a string at runtime ?
I was able to call a static function in the superclass using the following code:
var c:ChildClass = new ChildClass();
var s:String = getQualifiedSuperclassName(c);
var cl:Class = getDefinitionByName(s) as Class;
cl.foo.call();
//cl["foo"].call();
A Class object has all of the static properties and methods of the Class, so this should be reliable. cl.foo returns a Function object that you can then .call().
You can get a reference to the instance's own class using the constructor property, but to access the ancestor classes, you have to use describeType and getDefinitionByName. These are powerful, but costly - so make sure you don't overuse this:
function callStaticAncestorProperty( instance:Object, staticProperty:String ):* {
var type:XML = describeType( instance );
var ret:* = instance.constructor[staticProperty];
for each(var extend:XML in type.extendsClass)
ret = ret ? ret : getStaticPropertyOrUndefined( extend, staticProperty );
return ret;
}
function getStaticPropertyOrUndefined( extend:XML, staticProperty:String ):* {
var clazz:Class = getDefinitionByName( extend.#type.toString().replace( "::", "." ) ) as Class;
return clazz[staticProperty] ? clazz[staticProperty] : undefined;
}
This checks if the class itself has the property, and then iterates over each super type. Note that the first value to be found will be returned, i.e. if both the subclass and a super class have this property, that of the subclass will be returned.
Edit
I only just realized you were asking about method calls, not properties. That works pretty much the same way:
function callStaticAncestorMethod( instance:Object, staticMethod:String ):void {
var type:XML = describeType( instance );
var method:Function = instance.constructor[staticMethod];
for each(var extend:XML in type.extendsClass)
method = method ? method : getStaticMethodOrUndefined( extend, staticMethod );
if (method) method();
}
function getStaticMethodOrUndefined( extend:XML, staticMethod:String ):Function {
var clazz:Class = getDefinitionByName( extend.#type.toString().replace( "::", "." ) ) as Class;
return clazz[staticMethod] ? clazz[staticMethod] : undefined;
}
Or (Based on Sam DeHaan answer's):
If Superclass and Descendant have both a String id property...
(getDefinitionByName(getQualifiedSuperclassName(Descendant))as Class).foo();
trace((getDefinitionByName(getQualifiedSuperclassName(Descendant))as Class).id);
Where :
// trace (Descendant.id);
// if private : compile time Error.
// 1178: Attempted access of inaccessible property id through a reference with static type Class.
var d:Descendant;
trace((getDefinitionByName("Descendant") as Class).id);
// output undefined if private : the value if public. But don't throw compile time Error.
(getDefinitionByName("Descendant") as Class).foo();
// Call static foo() from Descendant. // Throw a compile time Error if method is private
// trace (Superclass.id);
// if private : compile time Error.
// 1178: Attempted access of inaccessible property id through a reference with static type Class.
var s:Superclass;
trace((getDefinitionByName("Superclass") as Class).id);
// output undefined if private : the value if public. But don't throw compile time Error.
(getDefinitionByName("Superclass") as Class).foo();
// Call static foo() from Superclass. // Throw a compile time Error if method is private

Why does Json.NET serialization fail with [Serializable] and a lambda inside a read-only property?

According to these release notes, Json.NET now supports the SerializableAttribute:
Json.NET now detects types that have the SerializableAttribute and serializes all the fields on that type, both public and private, and ignores the properties.
I have the following sample code that throws a JsonSerializationException:
Error getting value from 'CS$<>9__CachedAnonymousMethodDelegate1' on
'ConsoleApplication1.MyType'.
If I comment the TotalWithLambda property, then the serialization succeeds as expected. In fact, I get the following results:
Leave [Serializable], leave TotalWithLambda: throws JsonSerializationException
Leave [Serializable], remove TotalWithLambda: serializes "myList" only
Remove [Serializable], leave TotalWithLambda: serializes "myList", "Total", and "TotalWithLambda"
Remove [Serializable], remove TotalWithLambda: serializes "myList" and "Total"
I understand all of these cases except the first one. Why does the combination of [Serializable] and a read-only property with a lambda in it cause this exception?
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
class Program
{
static void Main(string[] args)
{
var foo = new MyType();
foo.myList = new List<int>() { 0, 1, 2, 3 };
var returnVal = JsonConvert.SerializeObject(foo);
Console.WriteLine("Return: " + returnVal.ToString());
Console.ReadKey();
}
}
[Serializable]
class MyType
{
public IList<int> myList;
public int Total { get { return this.myList.Sum(); } }
public int TotalWithLambda { get { return this.myList.Sum(x => x); } }
}
}
I installed and used JustDecompile and found that the compiler adds a field and a method to the class when the lambda is uncommented:
public class MyType
{
[CompilerGenerated]
private static Func<int, int> CS$<>9__CachedAnonymousMethodDelegate1;
[CompilerGenerated]
private static int <get_TotalWithLambda>b__0(int x) { ... }
// ... plus the other class members ...
}
When the class has SerializableAttribute on it, Json.NET tries to serialize the private field, but can't since it's of type Func<int, int>. Removing the SerializableAttribute instructs Json.NET to ignore private fields and so it doesn't cause a problem.
Update: Json.NET 4.5 release 3 now makes this only a problem if you explicitly set IgnoreSerializableAttribute=false, or it can be resolved by adding the JsonObjectAttribute to the class..
I've changed IgnoreSerializableAttribute to true by default in release 3 which undoes the breaking change introduced in release 2 - http://json.codeplex.com/releases/view/85975
You can read more about it here - http://json.codeplex.com/discussions/351981