your code modified (to do somewhat what you want, some numbers may be off, but it's animated ;D)
update:Instead of :#!/usr/bin/perl -w use strict; use Tk; sub jump_it { my ($c, $thing) = @_; my ($old_x, $old_y) = $c->coords($thing); my $new_pos = $old_x == 10 ? 80 : 10; $c->coords($thing, $new_pos, $new_pos); } =head1 ORIGINAL C<slide_it> function sub slide_it { my ($c, $thing) = @_; for my $i (10..80) { $c->coords($thing, $i, $i); for (my $wait=20000; --$wait;){}; } } =cut sub slide_it # canvas, card { my ($c, $thing) = @_; $$c{__slide_for} = 80; $$c{__slide_count} = 0; $$c{__slide_timer} = $c->repeat(100, # miliseconds [ \&move_thing, $c, $thing, 2, # this many pixels X 2, # this many pixels Y ,] ,); } sub move_thing { my($c, $thing, $nX, $nY) = @_; if( $$c{__slide_count} > $$c{__slide_for} ) { $$c{__slide_timer}->cancel() } $$c{__slide_count}++; $c->move( $thing, #what? $nX, $nY, ,); } my $mw = MainWindow->new; my $c = $mw->Canvas(-relief => 'sunken', -bd => 2, -width => 200, -height => 200); my $image = $c->Photo(-file => "cards/ah.gif"); my $card = $c->create('image', 10, 10, -anchor => 'nw', -image => $image); my $j_btn = $mw->Button(-text => 'Jump It', -command => [\&jump_it, $c, $card] ); my $s_btn = $mw->Button(-text => 'Slide It', -command => [\&slide_it, $c, $card] ); $j_btn->pack; $s_btn->pack; $c->pack; MainLoop;
you're probably better off withmy $image = $c->Photo(-file => "cards/ah.gif"); my $card = $c->create('image', 10, 10, -anchor => 'nw', -image => $image);
That way when you implement a timer, you can store it in $$image{_timer} .... since you're going to have a different Photo object for each card (for the most part) ... anyway, not real important to your question (I'd probably have @cards which would hold about 52 Tk::Photo objects ....)my $image = $c->Photo(-file => "cards/ah.gif"); $$image{_id} = $c->create('image', 10, 10, -anchor => 'nw', -image => $image);
update: ++danger though I like my way more ;)
| ______crazyinsomniac_____________________________ Of all the things I've lost, I miss my mind the most. perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;" |
In reply to (crazyinsomniac) Re: Tk Canvas Animation
by crazyinsomniac
in thread Tk Canvas Animation
by dvergin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |