在Android中创建游戏引力(续)?

在Android中创建游戏引力(续)?,第1张

概述编辑:我看了看LogCat,它说它无法给com.example.playground充气.然后我意识到它需要我com.game.myapp.Playground.我改变它后它起作用了.我最近问为什么引力不能在我的Android应用程序(link)中工作,我仍然遇到麻烦.我将视图更改为“Playground”类,但现在它只是强制关闭.我究竟做错

编辑:我看了看LogCat,它说它无法给com.example.playground充气.然后我意识到它需要我com.game.myapp.Playground.我改变它后它起作用了.

我最近问为什么引力不能在我的Android应用程序(link)中工作,我仍然遇到麻烦.我将视图更改为“Playground”类,但现在它只是强制关闭.我究竟做错了什么?

package com.game.myapp;import androID.app.Activity;import androID.graphics.Canvas;import androID.graphics.Paint;import androID.os.Bundle;public class InGame extends Activity{Playground v;private int radius;    private int xposition;    private int yposition;    private Paint paint;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        // Todo Rewrite this, it sucks. SerIoUsly.        super.onCreate(savedInstanceState);        v = new Playground(this);        setContentVIEw(v);    }    public InGame(int x, int y, int radius, int color)    {         this.xposition = x; this.yposition = y; this.radius = radius;        paint = new Paint(color);    }    voID moveBall(int x, int y){         xposition = x; yposition =y;            }     voID onDraw(Canvas canvas){          canvas.drawCircle(xposition, yposition, radius, paint);    }    }

游乐场类:

package com.game.myapp;import androID.content.Context;import androID.graphics.Canvas;import androID.vIEw.VIEw;public class Playground extends VIEw{public static InGame ball;public Playground(Context context) {    super(context);    // Todo auto-generated constructor stub}    @OverrIDe   public voID onDraw(Canvas canvas)   {       super.onDraw(canvas);       if (ball != null ){           ball.onDraw(canvas);       }   }}

继承LogCat:

11-04 16:36:33.945: D/dalvikvm(13177): newInstance Failed: no <init>()11-04 16:36:33.949: D/AndroIDRuntime(13177): Shutting down VM11-04 16:36:33.949: W/dalvikvm(13177): threadID=1: thread exiting with uncaught exception (group=0x4001e578)11-04 16:36:33.953: E/AndroIDRuntime(13177): FATAL EXCEPTION: main11-04 16:36:33.953: E/AndroIDRuntime(13177): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.game.myapp/com.game.myapp.InGame}: java.lang.InstantiationException: com.game.myapp.InGame11-04 16:36:33.953: E/AndroIDRuntime(13177):    at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1573)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at androID.app.ActivityThread.access00(ActivityThread.java:117)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:935)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at androID.os.Handler.dispatchMessage(Handler.java:99)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at androID.os.Looper.loop(Looper.java:130)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at androID.app.ActivityThread.main(ActivityThread.java:3687)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at java.lang.reflect.Method.invokeNative(Native Method)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at java.lang.reflect.Method.invoke(Method.java:507)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:600)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at dalvik.system.NativeStart.main(Native Method)11-04 16:36:33.953: E/AndroIDRuntime(13177): Caused by: java.lang.InstantiationException: com.game.myapp.InGame11-04 16:36:33.953: E/AndroIDRuntime(13177):    at java.lang.class.newInstanceImpl(Native Method)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at java.lang.class.newInstance(Class.java:1409)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at androID.app.Instrumentation.newActivity(Instrumentation.java:1021)11-04 16:36:33.953: E/AndroIDRuntime(13177):    at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1565)11-04 16:36:33.953: E/AndroIDRuntime(13177):    ... 11 more

解决方法:

这是完整的代码.我刚刚写了这个并测试了它.有用.我为活动添加了一个计时器,每秒向右和向下移动球10个像素.请研究它,从中学习并根据您的需要进行调整.

球类.

package com.example;import androID.graphics.Canvas;import androID.graphics.Paint;public class Ball{    private int radius;    private int xposition;    private int yposition;    private int color;    private Paint paint;    public Ball(int x, int y, int radius, int color)    {        this.xposition = x; this.yposition = y; this.radius = radius;        paint = new Paint();        paint.setcolor(color);    }    int getX(){return this.xposition;}    int getY(){return this.yposition;}    voID moveBall(int x, int y){        xposition = x; yposition =y;    }    voID onDraw(Canvas canvas){        canvas.drawCircle(xposition, yposition, radius, paint);    }}

游乐场课

package com.example;import androID.content.Context;import androID.graphics.Canvas;import androID.util.AttributeSet;import androID.Widget.ImageVIEw;public class Playground extends ImageVIEw {    private Ball ball;    public Playground(Context context) {        this(context,null);    }    public Playground(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public Playground(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public voID setBall(Ball ball){        this.ball = ball;    }    @OverrIDe    public voID onDraw(Canvas canvas)    {        super.onDraw(canvas);        if (ball != null ){            ball.onDraw(canvas);        }    }}

活动类

package com.example;import androID.app.Activity;import androID.graphics.color;import androID.os.Bundle;import java.util.Timer;import java.util.TimerTask;public class MyActivity extends Activity {    /**     * Called when the activity is first created.     */    Playground playground;    Ball ball;    Timer myTimer;    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        playground = (Playground) findVIEwByID(R.ID.playground);        ball = new Ball(100, 100, 20, color.RED);        playground.setBall(ball);        myTimer = new Timer();        myTimer.schedule(new TimerTask() {            @OverrIDe            public voID run() {                Update();            }        }, 0, 1000);    }    private voID Update() {        this.runOnUiThread(moveBall);    }    private Runnable moveBall = new Runnable() {        public voID run() {            ball.moveBall(ball.getX() + 10, ball.getY() + 10);            playground.invalIDate();        }    };}

[编辑] XML

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"              androID:orIEntation="vertical"              androID:layout_wIDth="fill_parent"              androID:layout_height="fill_parent">    <com.example.Playground            androID:ID="@+ID/playground"            androID:layout_wIDth="fill_parent"            androID:layout_height="fill_parent"/></linearLayout>

OOP的关键点.

Ball不了解游乐场或活动.它有一个方法,可能会被要求它在画布上绘制的东西调用但是它知道它可能是一个不可见的画布,或按钮的画布,或位图的画布 – 它不知道或需要知道.这是所谓的方法担心的任务.

游乐场不了解活动或球.它知道它可能有一个Ball类的实例,如果它有,它应该调用它的onDraw方法,但它不知道球是什么或它绘制什么. Ball担心这一点.

活动不知道Ball或Playground,除了它有一个,并调用Ball移动方法然后告诉Playground重绘自己.

关键是,您可以更改绘制方法,移动方法和其他所有内容而无需重新编码(一般情况下都可以).例如,您可以更改Ball类中的onDraw以绘制矩形.当然,班级的名字现在是一个糟糕的选择,但你明白了……

总结

以上是内存溢出为你收集整理的在Android中创建游戏引力(续)?全部内容,希望文章能够帮你解决在Android中创建游戏引力(续)?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110054.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存