android 代码植入,android原生植入RN

一、导入js

生成package.json文件

在项目根目录下运行(弹出对话,只管回车就行):

AppledeMacBook-Pro:00 macbook$ npm init

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields

and exactly what they do.

Use `npm install --save` afterwards to install a package and

save it as a dependency in the package.json file.

Press ^C at any time to quit.

name: (00)

version: (1.0.0)

description:

entry point: (index.js)

test command:

git repository:

keywords:

author:

license: (ISC)

About to write to /Users/macbook/work/react-native/00/package.json:

{

"name": "00",

"version": "1.0.0",

"description": "",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "",

"license": "ISC"

}

Is this ok? (yes)

在package.json文件中加入启动命令

"start": "node node_modules/react-native/local-cli/cli.js start"

下载react-native

运行:

npm install --save react-native

curl -o .flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig

新建js入口文件--index.android.js

'use strict';

import React from 'react';

import {

AppRegistry,

StyleSheet,

Text,

View

} from 'react-native';

class HelloWorld extends React.Component {

render() {

return (

Hello, 1

)

}

}

var styles = StyleSheet.create({

container: {

flex: 1,

justifyContent: 'center',

},

hello: {

fontSize: 20,

textAlign: 'center',

margin: 10,

},

});

AppRegistry.registerComponent('HelloWorld', () => HelloWorld);

二、配置项目

项目添加react-native依赖

在build.gradle(Module: app)中添加

compile "com.facebook.react:react-native:+" // From node_modules

添加RN maven 入口

在build.gradle(Project: [ProjectName])中添加

allprojects {

repositories {

...

maven {

// All of React Native (JS, Android binaries) is installed from npm

url "$rootDir/node_modules/react-native/android"

}

}

}

开启reload模式

在AndroidManifest.xml中添加

三、js入口

新建一个activity

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.KeyEvent;

import com.facebook.react.LifecycleState;

import com.facebook.react.ReactInstanceManager;

import com.facebook.react.ReactRootView;

import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;

import com.facebook.react.shell.MainReactPackage;

public class MainActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {

private ReactRootView mReactRootView;

private ReactInstanceManager mReactInstanceManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//创建一个ReactRootView,把它设置成Activity的主视图

mReactRootView = new ReactRootView(this);

mReactInstanceManager = ReactInstanceManager.builder()

.setApplication(getApplication())

.setBundleAssetName("index.android.bundle")

.setJSMainModuleName("index.android")

.addPackage(new MainReactPackage())

.setUseDeveloperSupport(BuildConfig.DEBUG)

.setInitialLifecycleState(LifecycleState.RESUMED)

.build();

mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", null);

setContentView(mReactRootView);

}

@Override

public void invokeDefaultOnBackPressed() {

super.onBackPressed();

}

//传递一些Activity的生命周期事件到ReactInstanceManager

//这是的JavaScript代码可以控制当前用户按下返回按钮的时候作何处理(譬如控制导航切换等等)。

//如果JavaScript端不处理相应的事件,你的invokeDefaultOnBackPressed方法会被调用。

//默认情况,这会直接结束你的Activity。

@Override

protected void onPause() {

super.onPause();

if (mReactInstanceManager != null) {

mReactInstanceManager.onPause();

}

}

@Override

protected void onResume() {

super.onResume();

if (mReactInstanceManager != null) {

mReactInstanceManager.onResume(this, this);

}

}

@Override

public void onBackPressed() {

if (mReactInstanceManager != null) {

mReactInstanceManager.onBackPressed();

} else {

super.onBackPressed();

}

}

//我们需要改动一下开发者菜单。

//默认情况下,任何开发者菜单都可以通过摇晃或者设备类触发,不过这对模拟器不是很有用。

//所以我们让它在按下Menu键的时候可以显示

@Override

public boolean onKeyUp(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {

mReactInstanceManager.showDevOptionsDialog();

return true;

}

return super.onKeyUp(keyCode, event);

}

创建上面activity的节点

android:name=".MyReactActivity"

android:label="@string/app_name"

android:theme="@style/Theme.AppCompat.Light.NoActionBar">

运行

npm start

报错

Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed with multiple

原因:导入的库在build.gradle中的minSdkVersion与你的应用的minSdkVersion不匹配

解决:app要求应用最小系统版本和库要求系统最小版本不一致,改成一样的就行了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里提供一个简单的Java游戏代码示例,演示如何植入NPC并与NPC进行对话: ``` import java.util.Scanner; public class Game { public static void main(String[] args) { // 创建NPC对象 NPC npc = new NPC("Bob", "Hello, I'm Bob. What can I do for you?"); // 添加NPC到游戏场景中 Scene scene = new Scene("Village"); scene.addNPC(npc); // 玩家与NPC交互 Scanner scanner = new Scanner(System.in); System.out.println("You are in " + scene.getName() + "."); System.out.println("You see " + npc.getName() + "."); System.out.println("Press '1' to talk to " + npc.getName() + "."); int choice = scanner.nextInt(); if (choice == 1) { System.out.println(npc.getDialog()); System.out.println("Press '1' to ask for a quest, or '2' to leave."); choice = scanner.nextInt(); if (choice == 1) { Quest quest = npc.generateQuest(); System.out.println("You get a new quest: " + quest.getName()); } else if (choice == 2) { System.out.println("You leave " + scene.getName() + "."); } } else { System.out.println("You leave " + scene.getName() + "."); } } } class NPC { private String name; private String dialog; public NPC(String name, String dialog) { this.name = name; this.dialog = dialog; } public String getName() { return name; } public String getDialog() { return dialog; } public Quest generateQuest() { return new Quest("Fetch water"); } } class Scene { private String name; private NPC[] npcs; public Scene(String name) { this.name = name; this.npcs = new NPC[0]; } public String getName() { return name; } public void addNPC(NPC npc) { NPC[] newNpcs = new NPC[npcs.length + 1]; for (int i = 0; i < npcs.length; i++) { newNpcs[i] = npcs[i]; } newNpcs[npcs.length] = npc; npcs = newNpcs; } } class Quest { private String name; public Quest(String name) { this.name = name; } public String getName() { return name; } } ``` 在这个示例中,我们创建了一个NPC类,包含名字和对话内容属性,以及生成任务的方法。我们还创建了一个场景类,包含名字和NPC数组属性,并提供了添加NPC的方法。在游戏主函数中,我们创建了一个场景对象和一个NPC对象,将NPC添加到场景中,并实现了玩家与NPC的交互,当玩家选择与NPC对话时,会显示NPC的对话内容,并提供选择是否接受任务的选项。如果玩家选择接受任务,会生成一个新的任务对象并显示在控制台上。 当然,这只是一个简单的示例,实际的Java游戏中需要更加复杂的逻辑和交互方式,但这个示例可以作为一个参考,帮助你了解如何在Java游戏中植入NPC并与其进行对话。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值