I am a newer in Cocos2dx JavaScript and I am gonna make a simple tetris app using React Native + Cocos2dx.
Also, I found an example which using ‘npm install @remobile/react-native-cocos2dx --save’ but it doesn’t work because it couldn’t recognize ‘cc.xxx’.
Here is the sample code.
'use strict';
var React = require('react');
var ReactNative = require('react-native');
var {
StyleSheet,
View,
} = ReactNative;
var resolveAssetSource = require('resolveAssetSource');
var Cocos2dx = require('@remobile/react-native-cocos2dx');
module.exports = React.createClass({
renderCocos2dx() {
return (res, transProps)=> {
cc.game.onStart = function(){
var MyScene = cc.Scene.extend({
alert(transProps.text);
onEnter:function () {
this._super();
var size = cc.director.getWinSize();
var sprite = cc.Sprite.create(res.img.qq);
sprite.setPosition(size.width / 2, size.height / 2 - 200);
sprite.setScale(0.8);
this.addChild(sprite, 0);
var sprite = cc.Sprite.create(res.img.weixin);
sprite.setPosition(size.width / 2, size.height / 2);
sprite.setScale(0.8);
this.addChild(sprite, 0);
var label = cc.LabelTTF.create("Hello World", "Arial", 40);
label.setPosition(size.width / 2, size.height / 2 + 200);
label.setColor(255, 0,255);
this.addChild(label, 1);
}
});
cc.director.runScene(new MyScene());
};
cc.game.run();
}
},
render () {
const resource = {
img: {
qq: resolveAssetSource(require('./img/qq.img')).uri,
weixin: resolveAssetSource(require('./img/weixin.img')).uri,
}
};
return (
<View style={styles.container}>
<Cocos2dx
render={this.renderCocos2dx()}
resource={resource}
transProps={{text: 'I am from react-native'}}
width={sr.tw}
height={sr.tch}
/>
</View>
);
},
});
var styles = StyleSheet.create({
container: {
flex: 1
},
});
Looking forward to get an answer asap.
Thanks.