如何在LOVE 2D中创建步行动画(How to create a walking animation in LOVE 2D)
所以我想知道如何改变我创建的角色形象,取决于我按下/正在按下的键吗?
当按下“d”(或任何一个按键)时,我最终会看到一个步行动画,但是当刚按下“d”键时,他会静止不动。所有图像都已经创建好了。
我试过这个,但没有成功:
function love.load() if love.keyboard.isDown("a") then hero = love.graphics.newImage("/hero/11.png") elseif love.keyboard.isDown("d") then hero = love.graphics.newImage("/hero/5.png") elseif love.keyboard.isDown("s") then hero = love.graphics.newImage("/hero/fstand.png") elseif love.keyboard.isDown("w") then hero = love.graphics.newImage("/hero/1.png") end function love.draw() love.graphics.draw(background) love.graphics.draw(hero, x, y) end
So I was wondering how to change an image of character I've created depending on the key I've pressed/am pressing?
My ultimate going to to have a walking animation occuring when "d" (or any of the wasd keys) is pressed but then he stands still when the "d" key has just been pressed etc. All images have been created already.
I've tried this but it didn't work out:
function love.load() if love.keyboard.isDown("a") then hero = love.graphics.newImage("/hero/11.png") elseif love.keyboard.isDown("d") then hero = love.graphics.newImage("/hero/5.png") elseif love.keyboard.isDown("s") then hero = love.graphics.newImage("/hero/fstand.png") elseif love.keyboard.isDown("w") then hero = love.graphics.newImage("/hero/1.png") end function love.draw() love.graphics.draw(background) love.graphics.draw(hero, x, y) end
原文:https://stackoverflow.com/questions/9359858
最满意答案
您需要通过
share
threads::shared
共享$clients
threads::shared
:my $clients = &share({});
老式的语法是由于Perl的原型有文件记录的问题。 如果你至少有Perl 5.8.9 ,使用更好
my $clients = shared_clone({});
代替。
你也想用锁来保护
$clients
, 例如 ,my $clients_lock : shared; { lock $clients_lock; $clients->{time()} = fileno $connection; }
最后,因为
IO::Socket::INET
实例是Perl typeglobs,所以不能共享它们,所以应该将它们的套接字描述符(从fileno
)添加到$clients
,然后在必要时fdopen
套接字open my $fh, ">&=", $sockdesc or warn ...
下面的程序重复向其他连接的套接字发送入站数据:
#!/usr/bin/perl use strict; use IO::Socket; use threads; use threads::shared; use Thread::Queue; # init my $clients = &share({}); my $clients_lock : shared; my $queue = Thread::Queue->new; # thread that monitors threads->create("monitor"); # create the listen socket my $port = 12345; my $listenSocket = IO::Socket::INET->new( LocalPort => $port, Listen => 10, Proto => 'tcp', Reuse => 1 ); # make sure we are bound to the port die "Can't create a listening socket: $@" unless $listenSocket; print "Server ready. Waiting for connections on $port ... \n"; # wait for connections at the accept call while (my $connection = $listenSocket->accept) { # set client socket to non blocking my $nonblocking = 1; ioctl($connection, 0x8004667e, \\$nonblocking); # autoflush $connection->autoflush(1); # debug print "Accepted new connection\n"; # add to list { lock $clients_lock; $clients->{time()} = fileno $connection; } # start new thread and listen on the socket threads->create("readData", $connection); } sub readData { # socket parameter my ($client) = @_; # read client while (<$client>) { chomp; $queue->enqueue($_); } close $client; } sub monitor { # endless loop while (1) { # loop while there is something in the queue while ($queue->pending) { # get data from a queue my $data = $queue->dequeue; # loop all sockets { lock $clients_lock; while ( my ($key, $value) = each(%$clients) ) { # send to socket if (open my $fh, ">&=", $value) { print $fh "$data\n"; } else { warn "$0: fdopen $value: $!"; } } } } # wait 0,25 seconds select(undef, undef, undef, 0.25); } } close $listenSocket;
You need to share
$clients
viashare
fromthreads::shared
:my $clients = &share({});
The old-fashioned syntax is due to a documented issue with Perl’s prototypes. If you have at least Perl 5.8.9, use the nicer
my $clients = shared_clone({});
instead.
You also want to protect
$clients
with a lock, e.g.,my $clients_lock : shared; { lock $clients_lock; $clients->{time()} = fileno $connection; }
Finally, because
IO::Socket::INET
instances are Perl typeglobs, you can’t share them, so instead add their socket descriptors (fromfileno
) to$clients
and thenfdopen
the socket when necessary withopen my $fh, ">&=", $sockdesc or warn ...
The program below repeats inbound data to the other connected sockets:
#!/usr/bin/perl use strict; use IO::Socket; use threads; use threads::shared; use Thread::Queue; # init my $clients = &share({}); my $clients_lock : shared; my $queue = Thread::Queue->new; # thread that monitors threads->create("monitor"); # create the listen socket my $port = 12345; my $listenSocket = IO::Socket::INET->new( LocalPort => $port, Listen => 10, Proto => 'tcp', Reuse => 1 ); # make sure we are bound to the port die "Can't create a listening socket: $@" unless $listenSocket; print "Server ready. Waiting for connections on $port ... \n"; # wait for connections at the accept call while (my $connection = $listenSocket->accept) { # set client socket to non blocking my $nonblocking = 1; ioctl($connection, 0x8004667e, \\$nonblocking); # autoflush $connection->autoflush(1); # debug print "Accepted new connection\n"; # add to list { lock $clients_lock; $clients->{time()} = fileno $connection; } # start new thread and listen on the socket threads->create("readData", $connection); } sub readData { # socket parameter my ($client) = @_; # read client while (<$client>) { chomp; $queue->enqueue($_); } close $client; } sub monitor { # endless loop while (1) { # loop while there is something in the queue while ($queue->pending) { # get data from a queue my $data = $queue->dequeue; # loop all sockets { lock $clients_lock; while ( my ($key, $value) = each(%$clients) ) { # send to socket if (open my $fh, ">&=", $value) { print $fh "$data\n"; } else { warn "$0: fdopen $value: $!"; } } } } # wait 0,25 seconds select(undef, undef, undef, 0.25); } } close $listenSocket;
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如何将套接字保存在一个散列中,并从另一个线程循环它们?(How do I save sockets in a hash and loop over them from another thread?)[2024-02-29]
您需要通过share threads::shared共享$clients threads::shared : my $clients = &share({}); 老式的语法是由于Perl的原型有文件记录的问题。 如果你至少有Perl 5.8.9 ,使用更好 my $clients = shared_clone({}); 代替。 你也想用锁来保护$clients , 例如 , my $clients_lock : shared; { lock $clients_lock; $clients->{t ... -
无法在套接字列表的循环中获取套接字的DataOutputStream(Can't get sockets' DataOutputStream in loop for sockets list)[2022-10-29]
你的程序实际上运行正常。 唯一的问题是你用连续读取来阻止客户端。 因此,您实际上会看到在他们从阅读中解锁后您在其他客户端上发送的消息。 尝试以下方法: 运行服务器 运行Client1 运行Client2 从Client1发送消息(注意Client2现在因输入而被阻止) 从Client2发送消息 现在,您将看到先前从client2上的client1发送的消息。 但是如果您需要两个客户端一旦发送消息就接收消息,您需要创建另一个线程来连续获取更新。 我已经修改了你的客户端。 import java.io.Buff ... -
基本级别的事件循环如下所示: while getNextEvent (&event) { dispatchEvent (&event); } 换句话说,它只不过是一个从一些描述队列中不断检索事件的循环,然后将事件分派给事件处理过程。 您可能已经知道,但我只是在上下文中解释它。 就不同的服务器如何处理它而言,似乎每个在Apache中创建的新连接都有一个为其创建的线程,并且该线程负责该连接,而没有其他任何连接。 对于另外两个,很可能有一个“设置”数量的线程正在运行(尽管这可能实际上因负载而异),并且连 ...
-
这是一个解决方案,它使用一个简单的类和asyncio.Event()来计算活动作业的数量,并指示循环在所有作业完成时停止: import asyncio import random class UseCounter: def __init__(self, loop=None): self.loop = loop self.event = asyncio.Event(loop=loop) self.n = 0 # The number of ac ...
-
我在 1。5年前开了一个问题 ,将一个端口https://github.com/vaughan0/go-zmq/blob/master/channels.go引入pebbe / zmq4。 最终作者决定反对它,但我们已经在生产中使用它(在非常繁重的工作负载下)很长一段时间了。 这是必须添加到pebbe / zmq4包中的文件的要点 (因为它向Socket添加了方法)。 这可以通过Socket接收器上的方法取代Socket作为参数的方式重写,但由于我们无论如何都要提供代码,这是一个简单的方法。 基本用法是像普 ...
-
# set hash jack.set('cart:1', { 'token' => '456789'}.to_json) #get hash JSON.parse jack.get('cart:1') # set hash jack.set('cart:1', { 'token' => '456789'}.to_json) #get hash JSON.parse jack.get('cart:1')
-
您正在询问如何将特定解决方案(将套接字切换到线程)复制到更广泛的问题(如何编写可伸缩服务器)。 “每个插槽一个线程”设计仅适用于一种请求 - 回复模式,例如HTTP。 真正高容量的用例是用于数据分发(发布 - 订阅)或任务分发(管道)。 两者都不适合1对1型号。 当你学习一种新工具时,这是一个常见的错误,“这个工具如何做我的旧工具做的事情”,但你不会得到这样的好结果。 相反,花点时间真正了解该工具的工作原理,然后利用这些知识重新思考您的问题并为他们提供最佳解决方案。 You're asking how to ...
-
套接字 - 选择/线程/两者(Sockets - select / thread / both)[2022-06-06]
线程和select不是互斥的。 多线程是一种并行处理形式,允许单个进程以异步方式看似执行多个任务。 使用select允许程序监视文件描述符(例如,套接字),等待事件。 两者都可以(并且据我所知,经常)一起使用。 在网络服务器环境中,线程可用于为多个客户端提供服务,而select则用于使其中一个线程不会占用CPU时间。 想象一下,您正在从多个客户端接收数据。 一个线程正在等待来自client1的数据,这需要很长时间,同时,client2正在疯狂地发送数据。 你有三个选择: 没有select ,使用阻塞调用:阻 ... -
RAM然后连接到RAM。 没有硬盘涉及。 但是,如果物理内存变低,则包含通过套接字传来的数据的某些内存页面可能会被换出到磁盘。 除此之外,不涉及硬盘驱动器。 RAM to wire then to RAM. No HDD involved. Yet, if the physical memory goes low, some memory pages containing data that came via the socket might be swapped out to disk. Other th ...