首页 \ 问答 \ 如何在LOVE 2D中创建步行动画(How to create a walking animation in LOVE 2D)

如何在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
更新时间:2022-05-07 14:05

最满意答案

您需要通过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 via share from threads::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 (from fileno) to $clients and then fdopen the socket when necessary with

open 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;

相关问答

更多
  • 您需要通过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 ...
  • 你的程序实际上运行正常。 唯一的问题是你用连续读取来阻止客户端。 因此,您实际上会看到在他们从阅读中解锁后您在其他客户端上发送的消息。 尝试以下方法: 运行服务器 运行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 ...
  • 线程和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 ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)