我正在制作游戏,但在使用“相交”功能时遇到了问题。senario看起来像这样;我制作了一个游戏,其中玩家是一个矩形,目的是杀死敌人的矩形。敌人的“老板”矩形有两个“模拟”,它们模拟运动和攻击,这些模拟是由矢量驱动的。运动是水平地来回移动,进攻是垂直的,是冲锋。凸台还具有一个矩形目标区域,如果该区域被弯曲,则“凸台”将在整个屏幕上充电。现在问题来了,当我尝试使其达到目标时,如果玩家与矩形目标区域相交,“老板”将开始进攻/冲锋。第一次与玩家相交时,boss攻击/冲锋,但此后没有。
((我会在下面加上一些代码。如果我的英语不好,抱歉。)首先是主要的:
boss b; Character C; void setup(){ C = new Character(); b = new boss(); } void draw(){ if (play) { b.simulate(); //horizantal movement } if (b.start) { b.sim(); //boss vertical attack } if (b.intersects(C)){ play = false; b.start = true; } C.character(); //player b.bounce(); //makes it bounce if horizantal. and stop if vertical b.Display(); //boss b.display(); //boss target area }
接下来是老板:
class boss { int x = 10 ; int y = 10 ; boolean start = false; int RW = 50; int RH = 700; boolean up = false; boolean down = true; boss() { Location = new PVector( x+25, y ); //vector for the location of the boss Velocity = new PVector( 5, 0 ); // vector for horizaltal movement speed = new PVector( 0, 10 ); // vector for vertical down movement speed2 = new PVector(0, -10); // vector for vertical up movement } void bounce() { if ((Location.x == width) ||(Location.x == 0)) { //horizantal movement bounce on screen edge Velocity.x = Velocity.x * -1; } if ((Location.y == 650) || (Location.y == 0)) { start = false; //makes boss stop if reaches botton or top of the screen play = true; if (Location.y == 650) { RH = -700; up = true; down = false; //specificly if it reaches top of screen } if (Location.y == 0) { RH = 700; down = true; //specificly if it reaches bottom of screen up = false; } } } void simulate() { Location.add(Velocity); //simulates horizantal movement } void sim() { if (down) { Location.add(speed); //simulates up and down attacking movemnet } if (up) { Location.add(speed2); } } boolean intersects(Character C) { return Location.x < C.x + C.w && C.x < Location.x + RW && Location.y < C.y + C.h && C.y < Location.y + RH; //intersect between player and boss targeting area } void Display() { rect( Location.x, Location.y, 50, 50 ); //boss } void display() { rect( Location.x, Location.y+50, RW, RH ); //boss targeting area } }
如果有任何不清楚的地方,我很乐意消除任何混乱。 :)
我正在制作游戏,但在使用“相交”功能时遇到了问题。 senario看起来像这样;我制作了一个游戏,其中玩家是一个矩形,目的是杀死...
感谢您共享代码。这使它变得容易得多。