- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有四个 ImageIcons,
private ImageIcon characterIntro;
private ImageIcon characterIdle;
private ImageIcon characterAttack;
private ImageIcon characterJump;
Timer time;
并且它们在构造函数内使用 gif 进行初始化。
我知道如何使用我的paintComponent在屏幕上绘制它们,但是如何让它们每个都出现一定的时间?我想执行以下顺序:
通常我的paintComponent看起来像这样,并且它同时显示所有这些:
public void paintComponent (Graphics page)
{
super.paintComponent(page);
page.drawImage(background, 0, 0, null);
if(stage1_completed && stage2_completed && stage3_completed && stage4_completed)
{
characterIntro.paintIcon (this, page, char.x, char.y-10);
characterIdle.paintIcon (this, page, char.x, char.y);
characterAttack.paintIcon (this, page, char.x, char.y);
characterIdle.paintIcon (this, page, char.x, char.y);
characterJump.paintIcon (this, page, char.x, char.y);
characterIdle.paintIcon (this, page, char.x, char.y);
}
}
如何使用Timer
来达到我想要的效果?
谢谢:)
最佳答案
要使用具有不同延迟的Timer
,您可以创建如下方法:
public void scheduleTask(int delay){
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
timer.cancel();
doSomething();
}
} , delay, 1);
}
这将在毫秒内的延迟
过去后运行方法doSomething
。在 doSomething()
方法中,您更改应显示的 ImageIcon
并在下一个 delay
中再次调用 scheduleTask()
方法。
为了实现这一点,我将引入一个名为 Animation
的类,它保存您的一张图像及其应显示的持续时间。
class Animation{
public ImageIcon Image;
public int Duration;
public Animation(ImageIcon Image, int Duraction){
this.Image = Image;
this.Duration = Duraction;
}
}
使用此类,您可以轻松创建一系列动画作为数组。
private Animation[] Sequence = {
new Animation(characterIdle, 3000),
new Animation(characterAttack, 6000),
new Animation(characterIdle, 5000),
new Animation(characterJump, 3000),
new Animation(characterIdle, 3000)
};
注意我遗漏了你的角色介绍图片。这将是初始化,而不是序列的一部分。
在您的代码中,使用 private ImageIconactualIcon;
对象来保存要显示的实际图像以及在 doSomething()
方法中更改的图像,以及 private intactualSequenceId;
值来保存序列数组中的实际动画 ID。
现在,您可以使用 characterIntro
初始化您的 actualIcon
对象,并使用 scheduleTask(5000); 安排第一次图像更改;
并像这样实现您的 doSomething()
方法:
public void doSomething(){
// change the actual Image and schedule next image change
actualImage = Sequence[actualSequenceId].Image;
scheduleTask(Sequence[actualSequenceId].Duration);
// increment sequence Id
if(actualSequenceId + 1 == Sequence.length) actualSequenceId = 0;
else actualSequenceId++;
}
关于java - 如何在一定时间内按顺序显示每个图像图标? Java、定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41096652/
哪种定时器在性能方面更适合使用? Jquery 计时器或 Javascript 计时器。 具有计时器的页面没有任何 Jquery 代码。 谢谢 最佳答案 在仅使用计时器的页面上引用完整的 JQuery
R 语言有没有简单的方法来设置定时器功能?计时器函数是指位于 session 后台并每隔一段时间执行一次的函数。 干杯! 最佳答案 tcltk2 包中有 tclTaskSchedule 函数(和其
我想在点击发生后调用 setTimeout()。 如果用户在 300ms 过去之前再次点击,我想停止那个计时器,触发另一个函数并重新启动原来的计时器。 我知道 setTimeout() 但我不确定如何
请参阅下面的代码...它会在页面加载 + 8 秒后更改图像,然后继续每 1 秒更改一次。 setInterval(function(){ setTimeout(function(){
我正在尝试使用计时器来安排应用程序中的重复事件。但是,我希望能够实时调整事件触发的时间段(根据用户输入)。 例如: public class HelperTimer extends TimerTask
setTimeout()--用于指定在一段特定的时间后执行某段程序。 格式: [定时
setTimeout 和 clearTimeout 复制代码 代码如下: var obj = setTimeout(cb, ms); setTim
if(e.getSource()==continuous) { TimerTask task = new TimerTask() { public void run()
请谁能告诉我如何在 iPhone 的 cocos2d 中实现启动游戏的倒计时器。 我的意思是,按下“播放”时,一个新场景会出现,显示数字“3”、“2”、“1”,然后显示“GO!”一词。 最佳答案 来自
我正在制作一个计时器,而且效果很好。唯一的问题是,每过一秒,它就会在新行中打印剩余的时间(以秒为单位)。我该如何做到这一点,而不是打印一个新行,而只是改变当前行中显示的内容? 这就是我所拥有的...
这个问题在这里已经有了答案: Lua Program Delay (2 个答案) 关闭 7 年前。 我目前使用 Corona SDK,Lua 作为我的主要语言。我在使用此代码时遇到问题 - 当我运行
我正在制作一个计时器,而且效果很好。唯一的问题是,每过一秒,它就会在新行中打印剩余的时间(以秒为单位)。我该如何做到这一点,而不是打印一个新行,而只是改变当前行中显示的内容? 这就是我所拥有的...
到目前为止,我使用的每种方法都只是暂时卡住我的程序,但我希望游戏继续运行,我只希望盾牌 boolean 值在 X 时间内为 true,然后在时间到期后返回 false,有吗有办法做到这一点吗?谢谢。
我需要创建一个异步线程,它运行一次,延迟 2 分钟,并且可以随时终止。我看到了几种可能的解决方案: ScheduledExecutorService 和 FutureTask 允许我中断正在运行的任务
我开发了一个简单的应用程序并使用了计时器,但如果我多次运行计时器,计时器会丢弃此异常:线程“AWT-EventQueue-0”java.lang.IllegalStateException 中的异常:
我正在实现一个计时器: timer = new Timer(); timer.schedule(new TimerTask() { @Overr
我有一个有点复杂的 iOS 用户界面,我需要每秒重新加载 UICollectionView 的特定单元格以显示时间(有点像复杂的秒表),我还需要每秒做一些其他事情在这次通话中。 问题的第 1 部分 我
我一直在研究可用于 QueryPerformanceCounter()/QueryPerformanceFrequency() 的不同类型的计时器,在进一步研究之后,我发现了一个使用计时器类的例子..
我正在尝试以微秒为单位做一个计时器,但它不太管用。 #include #include #include using namespace std; int main () { struc
假设我有一个整数数组 int timeouts [] = {1000 , 2000 , 3000 , 3500}; 我想创建一个计时器,最多计时 3.5 秒,并在毫秒计数等于数组元素之一时调用相同的函
我是一名优秀的程序员,十分优秀!