- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在用 Java 试验基于 2D tile 的横向卷轴游戏,主要基于 David Brackeen 的“Developing Games in Java”中的代码和示例
目前 map 文件的大小为 100x100 block (每个 block 为 64x64 像素)。我已经将系统配置为仅显示玩家可见的图 block 。 Graphics 系统由一个 ScreenManager 类管理,该类返回当前 BufferStrategy 的图形对象,如下所示:
屏幕管理器.java
private GraphicsDevice device;
...
/**
* Gets the graphics context for the display. The
* ScreenManager uses double buffering, so applications must
* call update() to show any graphics drawn.
* <p>
* The application must dispose of the graphics object.
*/
public Graphics2D getGraphics(){
Window window = device.getFullScreenWindow();
if(window != null){
BufferStrategy strategy = window.getBufferStrategy();
return (Graphics2D)strategy.getDrawGraphics();
}
else{
return null;
}
}
来自此 ScreenManager 的图形在游戏循环中传递到 TreeRenderer 的绘制方法之后。
TreeMapRenderer.java
/**
Draws the specified TileMap.
*/
public void draw(Graphics2D g, TileMap map,
int screenWidth, int screenHeight, float fr)
{
Sprite player = map.getPlayer();
int mapWidth = tilesToPixels(map.getWidth());
int mapHeight = tilesToPixels(map.getHeight());
// get the scrolling position of the map
// based on player's position
int offsetX = screenWidth / 2 -
Math.round(player.getX()) - TILE_SIZE;
offsetX = Math.min(offsetX, 0);
offsetX = Math.max(offsetX, screenWidth - mapWidth);
// get the y offset to draw all sprites and tiles
int offsetY = screenHeight /2 -
Math.round(player.getY()) - TILE_SIZE;
offsetY = Math.min(offsetY,0);
offsetY = Math.max(offsetY, screenHeight - mapHeight);
// draw the visible tiles
int firstTileY = pixelsToTiles(-offsetY);
int lastTileY = firstTileY + pixelsToTiles(screenHeight) +1;
int firstTileX = pixelsToTiles(-offsetX);
int lastTileX = firstTileX +
pixelsToTiles(screenWidth) + 1;
//HERE IS WHERE THE SYSTEM BOGS dOWN (checking ~280 tiles per iteration)
for (int y=firstTileY; y<lastTileY; y++) {
for (int x=firstTileX; x <= lastTileX; x++) {
if(map.getTile(x, y) != null){
Image image = map.getTile(x, y).getImage();
if (image != null) {
g.drawImage(image,
tilesToPixels(x) + offsetX,
tilesToPixels(y) + offsetY,
null);
}
}
}
}
// draw player
g.drawImage(player.getImage(),
Math.round(player.getX()) + offsetX,
Math.round(player.getY()) + offsetY,
null);
该算法可以正确地为 X 轴和 Y 轴选择正确的 FROM 和 TO 值,从 10000 到 ~285 之间剔除所需的图 block 。
我的问题是即使这样,游戏在渲染图 block 时也只能以大约 8-10 FPS 的速度运行。如果我关闭 tile 渲染,系统会以 80 FPS 运行(当无事可做时很容易快速运行)
您对如何加快这一过程有什么想法吗?我希望看到至少在 30 FPS 左右的东西才能让它可以玩。
最后,虽然我愿意使用第 3 方库来做到这一点,但我想在承认失败之前尝试自己实现这个逻辑。
编辑:
根据此处的要求,提供了关于如何调用 Image image = map.getTile(x, y).getImage();
的额外信息。
这里的 map 来自下面的TileMap类
TileMap.java
public class TileMap {
private Tile[][] tiles;
private LinkedList sprites;
private Sprite player;
private GraphicsConfiguration gc;
/**
Creates a new TileMap with the specified width and
height (in number of tiles) of the map.
*/
public TileMap(GraphicsConfiguration gc, int width, int height) {
this.gc = gc;
tiles = new Tile[width][height];
overlayer = new Tile[width][height];
sprites = new LinkedList();
}
/**
Gets the width of this TileMap (number of tiles across).
*/
public int getWidth() {
return tiles.length;
}
/**
Gets the height of this TileMap (number of tiles down).
*/
public int getHeight() {
return tiles[0].length;
}
/**
Gets the tile at the specified location. Returns null if
no tile is at the location or if the location is out of
bounds.
*/
public Tile getTile(int x, int y) {
if (x < 0 || x >= getWidth() ||
y < 0 || y >= getHeight())
{
return null;
}
else {
return tiles[x][y];
}
}
/**
* Helper method to set a tile. If blocking is not defined than it is set to false.
*
* @param x
* @param y
* @param tile
*/
public void setTile(int x, int y,Image tile){
this.setTile(x,y,tile,false);
}
/**
Sets the tile at the specified location.
*/
public void setTile(int x, int y, Image tile, boolean blocking) {
if(tiles[x][y] == null){
Tile t = new Tile(gc, tile, blocking);
tiles[x][y] = t;
}
else{
tiles[x][y].addImage(tile);
tiles[x][y].setBlocking(blocking);
}
}
...
此处的 Tile 是以下代码的实例。本质上,此类仅包含图像,可以通过始终使用 gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT) 添加覆盖层来对其进行更新;和一个 boolean 值来判断它是否会阻止玩家。传入的图像也是以这种方式创建的。
瓷砖.java
public class Tile {
private Image image;
private boolean blocking = false;
private GraphicsConfiguration gc;
/**
* Creates a new Tile to be used with a TileMap
* @param image The base image for this Tile
* @param blocking Will this tile allow the user to walk over/through
*/
public Tile(GraphicsConfiguration gc, Image image, boolean blocking){
this.gc = gc;
this.image = image;
this.blocking = blocking;
}
public Tile(GraphicsConfiguration gc, Image image){
this.gc = gc;
this.image = image;
this.blocking = false;
}
/**
Creates a duplicate of this animation. The list of frames
are shared between the two Animations, but each Animation
can be animated independently.
*/
public Object clone() {
return new Tile(gc, image, blocking);
}
/**
* Used to add an overlay to the existing tile
* @param image2 The image to overlay
*/
public void addImage(Image image2){
BufferedImage base = (BufferedImage)image;
BufferedImage overlay = (BufferedImage)image2;
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(base.getWidth(), overlay.getWidth());
int h = Math.max(base.getHeight(), overlay.getHeight());
//BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
BufferedImage combined = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);
this.image = (Image)combined;
}
public boolean isBlocking(){
return this.blocking;
}
public void setBlocking(boolean blocking){
this.blocking = blocking;
}
public Image getImage(){
return this.image;
}
最佳答案
我会使用像素渲染引擎(google it ;D)
基本上你所做的,它有一个巨大的整数数组,对应于你正在绘制的图像。
基本上,每个图 block 都有一个代表其像素的整数数组。当您渲染该图 block 时,您将图 block 数组“复制”(比这稍微复杂一点)到大数组:)
然后,一旦你完成了对主数组的所有渲染,你就可以在屏幕上绘制它。
这样,您每次画东西时都只处理整数而不是整张图片。这使其速度更快。
我使用 MrDeathJockey 的(youtube)教程并将它们与 DesignsbyZephyr 的(也是 youtube)相结合来学习这一点。虽然我不推荐使用他的技术(他只使用 4 种颜色和 8 位图形,与 deathJockey 的教程一样,你可以自定义图像的大小,甚至有多个不同分辨率的 Sprite 表(对字体有用)
但是我确实使用了一些偏移的东西(让屏幕而不是播放器移动)和 Zephyr 的 InputHandler :)
希望对您有所帮助!-Camodude009
关于基于 Java Tile 的游戏性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10046401/
这是引用 Google Tile Map 或 Bing Maps。是否可以在不使用任何类型的内部计算指定缩放级别(或 LevelOfDetails)的情况下获得 Tile Count、Tile X、T
我有一个 Tiles 模板,其中每个页面都提供一个标题,一些要添加到 中的内容,要放入一些混凝土的东西 ,以及附加到 的内容在一切之后。 大部分内容都非常小,因为页面是用 JS 呈现的。 我怎样才
我正在使用 Apache Tiles 2.1 开展一个项目。 我遇到了一个问题,即使用列表属性扩展模板会创建这些列表项的重复项...每个继承级别都有一组重复项。 作为示例,以下是基本定义及其将生成的页
我在 HTML 的背景层中有一个重复的水平图案。我可以使用 1 像素宽的图像或它的倍数(10、20、50...)来实现。 问题是: 哪个更好? 使用薄(小文件大小)图像并使其重复很多 使用更大的图片,
我们正在使用 Apache Tiles 3.0。 在我们的 Apache Tiles-Def 文件中,我们偶尔会遇到此 DTD 引用的问题,这可能是因为该站点不可靠。偶尔会出现“Reading Def
我在我的网络应用程序中使用 Tiles。我在瓷砖中使用了标准布局 (standard.jsp)。在 standard.jsp 之上有很多包含,涉及标签库等。 让我们做一个简化的例子。 标准.jsp:
我正在制作一个基于 2d tile 的游戏,我试图通过对 tile 结构中的值使用位字段和字节来保持我的 tile 结构小: struct Tile { // 3 bytes (24 bits
我已经在 Tiled 中制作了一张 map ,并且已经生成了一个 JSON。每当我尝试在 Chrome 中加载 map 时,它根本无法加载。当我去检查网站时,有类似警告: 在来自 Tiled 的 Js
我使用 struts 2.3.16 和 tiles 2.0.6。 具有空属性(以及其他属性)的图 block 定义: 一些其他定义扩展了它,他们可以选择填入值: 或者也留空。 我试图通过使用 st
我想在Struts2框架中使用tile。作为用于显示图 block 内容的标签,tiles:insert 和tiles:get 之间的基本区别是什么? 最佳答案 is equivalent to
我正在从 Strut1 + Tiles 项目迁移到 SpringMVC 和 Apache Tiles 3。我对Struts1+Tiles只了解一点点,它太旧了,我陷入了Struts-tiles中的Co
我正在使用Tiles,Spring和Hibernate创建一个应用程序。 在运行时,它显示以下错误: Can not find the tag library descriptor for "http
目前我在计划任务的帮助下实现了我的翻转图块,因此类似方法的一个问题是翻转图块将在时间间隔内翻转相同的图像。所以我想要实现的是我需要像循环瓷砖一样翻转我的图像。即一个接一个的方式。这里要注意的一件事是我
我正在使用 v0.6.2 和 Cocos2D v0.99.5,现在我真的非常需要为我的一些图 block 制作动画。我读了part of a wiki关于如何为图 block 制作动画,但它似乎与 M
我有一个图 block ,其中列出了提供给它的对象中的文章。此图 block 是大多数页面的一部分,但不是全部页面。有没有什么方法可以自动仅向需要它的页面提供对象(包含特定图 block )?现在我只
我正在寻找一种解决方案,以根据游戏 block 的类型对我的游戏 block 进行分组。瓷砖存储在二维数组中,类型为空和水,分组的瓷砖将存储在组类中。 所以如果我有二维数组: 0, 0, 0, 0,
我在今天的 maven 构建过程中遇到以下错误。 Unable to resolve artifact: Unable to get dependency information: Unable to
我正在尝试让 Apache Tiles' put-list-attribute 与 Thymeleaf 一起工作。 这是我尝试过的: 来自 Tiles 配置: 来自 thymlea
我在 UI 的 mat-grid-list 中动态显示 mat-grid-tile,其数量和数据随着后端值的变化而变化。它们是动态生成的。单击任何 mat-grid-tile 都会调用一个函数并将数据
我制作了一个 Tiled 游戏。现在,我正在通过增加场景中的节点数量来对手机的功能进行压力测试。有基于物理的东西,AI 运动,日夜系统,粒子在这里和那里突然出现以及我的场景的引擎盖下发生的许多其他事情
我是一名优秀的程序员,十分优秀!