libgdx学习记录5——演员Actor
Actor也是libgdx中非常重要的一个元素,一般与stage配合一起使用。Actor能够设置大小,位置,旋转和动画等。
我们自定义的Actor一般需要继承于Actor,并且重写其中的act和draw方法。
自定义的actor是一个图片。
class MyActor extends Actor{
TextureRegion region;
public MyActor(){
Texture texture = new Texture( Gdx.files.internal( "data/badlogic.jpg" ) );
region = new TextureRegion( texture );
setSize( region.getRegionWidth()/2, region.getRegionHeight()/2 );
setOrigin( getWidth()/2, getHeight()/2 );
}
@Override
public void act(float delta) {
// TODO Auto-generated method stub
super.act(delta);
}
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
// TODO Auto-generated method stub
batch.draw( region, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation() );
super.draw(batch, parentAlpha);
}
public void dispose(){
region.getTexture().dispose();
}
}
主类,包含stage:
package com.fxb.newtest;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image; public class Lib004_Actor extends ApplicationAdapter{ BitmapFont font;
Stage stage;
MyActor actor;
//String strShow;
int count; @Override
public void create() {
// TODO Auto-generated method stub
stage = new Stage();
font = new BitmapFont();
font.setColor( Color.DARK_GRAY ); actor = new MyActor();
stage.addActor( actor );
actor.setPosition( stage.getWidth()/2-actor.getWidth()/2, stage.getHeight()/2-actor.getHeight()/2 ); count = 0;
actor.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
count++;
return true;
}
}); Gdx.input.setInputProcessor( stage );
} @Override
public void render() {
// TODO Auto-generated method stub
Gdx.gl.glClearColor( 1, 1, 1, 1 );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); stage.act();
stage.draw(); SpriteBatch batch = stage.getSpriteBatch();
batch.begin();
//batch.draw( font, actor.getX(), );
font.draw( batch, "You have clicked " + count + " times!", actor.getX()-25, actor.getY()-20 );
batch.end(); } @Override
public void dispose() {
// TODO Auto-generated method stub
super.dispose();
} }
运行结果:

libgdx学习记录5——演员Actor的更多相关文章
- libgdx学习记录6——动作Action
libgdx中的Action类能够有效的帮助我们实现位移.旋转.缩放.淡入淡出等效果,对游戏的设计很有用. Action是一个抽象类,本身不可以实例化.一般使用的它的继承类,常用的有 MoveToAc ...
- libgdx学习记录2——文字显示BitmapFont
libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...
- libgdx学习记录3——动画Animation
libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...
- libgdx学习记录26——Polygon多边形碰撞检测
libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...
- libgdx学习记录22——3d物体创建
libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...
- libgdx学习记录20——多线程MultiThread资源处理
在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...
- libgdx学习记录19——图片动态打包PixmapPacker
libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...
- libgdx学习记录18——Box2d物理引擎
libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...
- libgdx学习记录17——照相机Camera
照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终.一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动.元素的放大.旋转等操作. C ...
随机推荐
- Python 面向对象补充
什么是面向对象编程 类 + 对象 class 类: def 函数1(): pass def 函数2(): pass obj是对象, 实例化的过程 obj = 类() obj.函数1() 例1 , 某些 ...
- bootstrap使用基础
1.为了适应跨屏浏览,Bootstrap为单元格预定义了4种class ,分别对应于手机.ipad.笔记本电脑.台式机. <div class="row"> <d ...
- Oracle EBS INV 释放保留
CREATE or REPPLACE PROCEDURE RelieveReservation AS -- Common Declarations l_api_version NUMBER := 1. ...
- MySQL 8.0.2: Introducing Window Functions
July 18, 2017MySQL, SQLDag Wanvik MySQL 8.0.2 introduces SQL window functions, or analytic functions ...
- ssh中文手册
ssh-keygen 中文手册 sshd_config 中文手册 sshd 中文手册
- CameraAPI中的 自定义照相功能
前几天的项目需要使用CameraAPI自己定义照相机,之前用过的二维码也要自己写底层代码,于是总结一下使用CameraAPI的几点事项.现在由于JDK7.0及其以上版本的官方文档已经不再推荐使用cam ...
- MySQL一个延迟案例
突然接到报警显示MySQL主从之间延迟过大,随后尽快到集群上面看看,进行排查. 首先我们查看延迟是由什么造成的,排查一遍过后发现不是网卡和从库机器的负载,那就要从其他地方来排除了 查看binlog日志 ...
- PowerShell “execution of scripts is disabled on this system.”
Set-ExecutionPolicy RemoteSigned
- .后面是方法不加引号 ,后面是"名"要加引号
.后面是方法不加引号 ,后面是"名"要加引号
- [Python2]介绍关于Uiautomator的watcher使用场景及使用方法
[官方的介绍]: Watcher You can register watcher to perform some actions when a selector can not find a mat ...