Sprite Move Demo


Cause a sprite to move from its present location to any chosen spot in the grahicbox.

nomainwin
    notice "Click in window as many times as you want, to make sprite move there"
    endX=50:endY=50     'initial location of sprite

    'load the bitmaps we'll use as sprites & background
    loadbmp "smiley1", "sprites\smiley1.bmp"
    loadbmp "smiley2", "sprites\smiley2.bmp"
    loadbmp "smiley3", "sprites\smiley3.bmp"
    loadbmp "smiley4", "sprites\smiley4.bmp"
    loadbmp "landscape", "sprites\bg1.bmp"

    'open a window and graphicbox
    WindowHeight = 300
    WindowWidth = 400
    graphicbox #w.g, 0, 0, 400, 300
    open "sprite test" for window_nf as #w
    print #w, "trapclose [quit]"

    'add a background and sprite
    print #w.g, "background landscape";
    print #w.g, "addsprite smiley smiley1 smiley2 smiley3 smiley4";
    print #w.g, "cyclesprite smiley 1"
    print #w.g, "spritexy smiley ";endX;" ";endY

    'tell the sprite to cycle one frame at a time, and move x5, y5
    print #w.g, "spritemovexy smiley 5 5"
    print #w.g, "drawsprites"
    print #w.g, "setfocus; when leftButtonDown [click]"
    cursor crosshair
    timer 250, [doFrame]
    wait

[click]
    timer 0
    endX=MouseX - 10    'to make it inside sprite
    endY=MouseY - 10
    print #w.g, "spritexy? smiley startX startY"

    'how many steps do we need?
    'pythagorean theorem
    'the *divide by 10* below can be changed to make sprite move
    'in larger or smaller increments
    steps = int(sqr((endX-startX)^2 + (endY-startY)^2)/10)
    'work out how far to move each time:
    xTotal=endX-startX
    xMove=int(xTotal/steps)    'make 20 steps
    yTotal=endY-startY
    yMove=int(yTotal/steps)    'make 20 steps
    #w.g "spritemovexy smiley ";xMove;" ";yMove
    timer 250, [doFrame]
    wait

[doFrame]
    'get current x position of sprite:
    print #w.g, "spritexy? smiley nowX nowY"

    'see if sprite has gotten to end X position:
    if xMove<0 then
        if nowXendX then
            print #w.g, "spritemovexy smiley 0 0"
        end if
    end if

    'see if sprite has gotten to end Y position:
    if yMove<0 then
        if nowYendY then
            print #w.g, "spritemovexy smiley 0 0"
        end if
    end if


    print #w.g, "drawsprites";
    wait

[quit]
    unloadbmp "smiley1"
    unloadbmp "smiley2"
    unloadbmp "smiley3"
    unloadbmp "smiley4"
    unloadbmp "landscape"
    close #w:end


Home

Source Code

Utilities

Internet

Games

Graphics

Media Demos

Snippets

DLL's

API Resources

Freeware

LB 4 Companion

Mastering LB 3

LB Workshop

Game Workshop

Links

Index