Because of... everything? Awesome, I'll just read the two tutorials about lexical scoping and clusures and... wait? What? No idea what I'm supposed to be learning here.
In the hopes to have learned something from reading, the methods I created are outside of the closures of my main file?
I'm having trouble relating how these tutorials affect A. My having to use the sub keyword... it IS a sub already and B. Why I must pass @_ to the sub when it was implicit before in this example:
$app->add_event_handler( \&quit_event );
$app->add_move_handler ( \&calculate_laser );
$app->add_show_handler ( \&render_laser );
sub quit_event
{
my $event = shift;
my $controller = shift;
$controller->stop if $event->type == SDL_QUIT;
}
sub calculate_laser
{
# The step is the difference in Time calculated for the next jump
my ( $step, $app, $t ) = @_;
$laser += $velocity * $step;
$phaser += $velocity * $step;
$velocity = -$velocity if $laser > $app->w;
$velocity = -$velocity if $laser < -20;
}
sub render_laser {
my ($delta, $app) = @_;
#draw bg
$app->draw_rect( [0, 0, $app->w, $app->h ], 0);
$app->draw_rect([$phaser, $app->h/4, 20, 4], [100, 100, 255, 255])
+;
$app->draw_rect( [ $laser, $app->h / 2, 20, 4 ], [255, 8, 8, 255]
+);
$app->update();
}
I'm fully aware that I may just be missing some major points on closures and lexical scoping, but after reading I'm just more confused as to what's going on rather than anything becoming clearer
Ransom
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.