Hey monks, back again with another puzzler.

I'm testing out SDL and SDLx, as well as learning Moose. According to the SDLx docs, when setting up move and show handler callbacks, SDLx will automatically pass some variables in. Namely, ($step, $app, $time) = @_;. This was all well and good when I was practicing outside of Moose. All values were passed correctly.

Once Moose was incorporated, I passed an object method as the callback instead of a sub in the main file. Now when the callback is called, I only receive the blessed hashref of the object, none of the $step, $app, or $time values. This effectively breaks my movement and drawing methods.

The main code is like this:
#! /opt/perl/bin/perl package game; use strict; use warnings; use SDL; use SDL::Event; use SDL::Events; use SDLx::App; use Switch; use Mob; my $app = SDLx::App->new( w => 400, h => 400, t => "Pew Pew", hw_surface => 1, double_buf => 1, dt => .1, delay => 10, ); SDL::Events::enable_key_repeat(10, 10); my $ship = Mob->new(); $app->add_event_handler(\&moveship); $app->add_move_handler ( \&{$ship->move} ); $app->add_show_handler ( \&{$ship->draw} ); sub moveship { my $event = shift; my $controller = shift; if ($event->type == SDL_KEYDOWN) { switch($event->key_sym()) { case SDLK_UP { $ship->{velocity}->{y} -= 1} case SDLK_DOWN { $ship->{velocity }->{y} += 1} case SDLK_LEFT{ $ship->{velocity }->{x} -= 1} case SDLK_RIGHT{ $ship->{velocity }->{x} += 1} } } } $app->run;
and the Moose class is as follows:
#! /opt/perl/bin/perl package Mob; use Moose; use Data::Dumper; has 'position' => (is => 'rw', builder => 'build_pos'); has 'velocity' => (is => 'rw', builder => 'build_vel'); sub build_pos { return {x => 0, y => 0}; } sub build_vel { return {x => 0, y => 0}; } sub move { print Dumper("Move", @_); } sub draw { print Dumper("Draw", @_); } 1;

As you can see, I've stripped the methods of anything useful in order to just print the argument lists. The counter example of this is to move the callback method to the main file, in which it will pass all the values the documentation specifies.

How can I get the values from SDLx app stuff into my Moose method calls?

Occasionally putting words into a post is beyond my ability so please don't hesitate to ask me for more information or a better description.

Ransom

In reply to SDLx handlers and Moose by Ransom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.