Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Too many ElseIf's, ideas?


Reply
 
Thread Tools Display Modes
  #1  
Old 04-23-2004, 03:17 AM
Dark_Neo Dark_Neo is offline
Regular
 
Join Date: Mar 2003
Posts: 60
Default Too many ElseIf's, ideas?


I have a function to move the pupils in 2 eyes (it's an animation program) the problem is there are too many ElseIf statments, surely there must be a better way. Currently I have it so that I have 4 sliders that control the posistion of each pupil (2 sliders for each pupil) each slider has 3 positions, making a total of 9 posistions for the pupil, here's the code I've got:

Code:
Private Sub MoveEyes(eye As Integer, x As Integer, y As Integer)
offset = 0
If eye = 1 Then offset = 1800
If x = 0 And y = 0 Then
    shpPupilsPreview(eye).Top = 960
    shpPupilsPreview(eye).Left = 600 + offset
ElseIf x = 1 And y = 0 Then
    shpPupilsPreview(eye).Top = 960
    shpPupilsPreview(eye).Left = 720 + offset
ElseIf x = 2 And y = 0 Then
    shpPupilsPreview(eye).Top = 960
    shpPupilsPreview(eye).Left = 840 + offset
ElseIf x = 0 And y = 1 Then
    shpPupilsPreview(eye).Top = 1080
    shpPupilsPreview(eye).Left = 600 + offset
ElseIf x = 1 And y = 1 Then
    shpPupilsPreview(eye).Top = 1080
    shpPupilsPreview(eye).Left = 720 + offset
ElseIf x = 2 And y = 1 Then
    shpPupilsPreview(eye).Top = 1080
    shpPupilsPreview(eye).Left = 840 + offset
ElseIf x = 0 And y = 2 Then
    shpPupilsPreview(eye).Top = 1200
    shpPupilsPreview(eye).Left = 600 + offset
ElseIf x = 1 And y = 2 Then
    shpPupilsPreview(eye).Top = 1200
    shpPupilsPreview(eye).Left = 720 + offset
ElseIf x = 2 And y = 2 Then
    shpPupilsPreview(eye).Top = 1200
    shpPupilsPreview(eye).Left = 840 + offset
End If
End Sub
But I'm thinking of changing it so that there are 5 posistions for each slider. Making 25 posistions, this would require a massive amount of ElseIf's using my method. Also it creates another problem since the pupil can't go outside the eye, which would happen if both sliders where at their extremes (like if both where at 0 or 5, or one was at 0 and the other at 5) I would be gratefull for any ideas
__________________
Dark Neo - Because good is just too boring
Reply With Quote
  #2  
Old 04-23-2004, 03:26 AM
Syko10-96's Avatar
Syko10-96 Syko10-96 is offline
Contributor
 
Join Date: Oct 2002
Location: Virginia, USA
Posts: 716
Default

Given the following based off of your If Then ElseIf's...

x = 0 then shpPupilsPreview(eye).Left = 600 + offset
x = 1 then shpPupilsPreview(eye).Left = 720 + offset
x = 2 then shpPupilsPreview(eye).Left = 840 + offset

AND

y = 0 then shpPupilsPreview(eye).Top = 960
y = 1 then shpPupilsPreview(eye).Top = 1080
y = 2 then shpPupilsPreview(eye).Top = 1200

The following Select Case pair should take care of your code, making it smaller and easier to read

Code:
Private Sub MoveEyes(eye As Integer, x As Integer, y As Integer) offset = 0 If eye = 1 Then offset = 1800 Select Case x Case 0 shpPupilsPreview(eye).Left = 600 + offset Case 1 shpPupilsPreview(eye).Left = 720 + offset Case 2 shpPupilsPreview(eye).Left = 840 + offset End Select Select Case y Case 0 shpPupilsPreview(eye).Top = 960 Case 1 shpPupilsPreview(eye).Top = 1080 Case 2 shpPupilsPreview(eye).Top = 1200 End Select End Sub

This could then easily be expanded to 5 positions apiece
__________________
I honestly think you ought to calm down; take a stress pill and think things over. - HAL 9000 (2001: A Space Odyssey)
Reply With Quote
  #3  
Old 04-23-2004, 03:26 AM
webbone's Avatar
webbone webbone is offline
Hydrogen Powered

Administrator
* Expert *
 
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
Default

Looking at what you have posted I see that X is an index into the array {600, 720, 840} and Y is an index into the array {960, 1080, 1200}.

You can further reduce these through a little math (Assuming the value for eye is either 0 or 1):

.Left = 600 + (Eye*1800) + (X*120)
.Top = 960 + (Y*120)

Which reduces your sub to:
Code:
Private Sub MoveEyes(eye As Integer, x As Integer, y As Integer) shpPupilsPreview(eye).Top = 960 + (Y*120) shpPupilsPreview(eye).Left = 600 + (Eye*1800) + (X*120) End Sub

You should be able to adjust the constants in these calculations to increase the number of steps in your sliders to just about anything - for example, instead of 3 positions, how about 30 positions?
Code:
Private Sub MoveEyes(eye As Integer, x As Integer, y As Integer) shpPupilsPreview(eye).Top = 960 + (Y*12) shpPupilsPreview(eye).Left = 600 + (Eye*1800) + (X*12) End Sub

EDIT: I believe that reduces the number of ElseIf's to 0!
__________________
"With the appearance of the AddressOf operator, an entire industry has developed among authors illustrating how to do previously impossible tasks using Visual Basic. Another industry is rapidly developing among consultants helping users who have gotten into trouble attempting these tasks." -Dan Appleman
Reply With Quote
  #4  
Old 04-23-2004, 03:28 AM
Syko10-96's Avatar
Syko10-96 Syko10-96 is offline
Contributor
 
Join Date: Oct 2002
Location: Virginia, USA
Posts: 716
Default

wow! guess I need more coffee nice one webbone!
__________________
I honestly think you ought to calm down; take a stress pill and think things over. - HAL 9000 (2001: A Space Odyssey)
Reply With Quote
  #5  
Old 04-23-2004, 03:29 AM
bk2003's Avatar
bk2003 bk2003 is offline
Contributor
 
Join Date: Aug 2003
Location: Gothenburg, Sweden
Posts: 648
Default

Code:
Private Sub MoveEyes(eye As Integer, x As Integer, y As Integer)
offset = eye * 1800


    shpPupilsPreview(eye).Top = 960 + (y * 120)
    shpPupilsPreview(eye).Left = 600 + (x*120) + offset
End Sub

Oops, way to late....
Reply With Quote
  #6  
Old 04-23-2004, 03:37 AM
webbone's Avatar
webbone webbone is offline
Hydrogen Powered

Administrator
* Expert *
 
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
Default

Quote:
Originally Posted by Dark_Neo
Also it creates another problem since the pupil can't go outside the eye, which would happen if both sliders where at their extremes (like if both where at 0 or 5, or one was at 0 and the other at 5)
I'm assuming this means you are moving the pupil around inside a circle (the eye) and don't want to allow the user to move it outside of that circle? What is the size/position of the circle? It should be possible to limit the position by testing the values the equations I provided you with against whether they fall inside the circle.

EDIT: For those of you wishing to think up a solution to generate the limiting, remember that a circle is defined by the equation X^2 + Y^2 = R, for a circle of Radius R and center at (0, 0).
__________________
"With the appearance of the AddressOf operator, an entire industry has developed among authors illustrating how to do previously impossible tasks using Visual Basic. Another industry is rapidly developing among consultants helping users who have gotten into trouble attempting these tasks." -Dan Appleman
Reply With Quote
  #7  
Old 04-23-2004, 04:02 AM
Dark_Neo Dark_Neo is offline
Regular
 
Join Date: Mar 2003
Posts: 60
Default

Wow, thanks! It makes so much sense now!

The outer circle is 855 (diameter)
The pupil circle is 375.

I think it would be quite easy to check the values. I could just add the X and Y together and if it's too high, subtract from one of them.....hmmmmm......
__________________
Dark Neo - Because good is just too boring
Reply With Quote
  #8  
Old 04-23-2004, 04:44 AM
Stroker Stroker is offline
Newcomer
 
Join Date: Apr 2004
Posts: 12
Default

Instead of x and y, why not rho and theta?

.top = cos(rho) * theta * stepmultipler1 + offset1
.left = sin(rho) * theta * stepmultiplier2 + offset2

Is that right?
Or did I get sin and cos mixed up?
(my trig is a bit rusty)

A polar approach would take care of the corner problem no prob.

Umm...
It's too early.
Reply With Quote
  #9  
Old 04-23-2004, 04:53 AM
Mandelbrot's Avatar
Mandelbrot Mandelbrot is offline
Senior Contributor
 
Join Date: Apr 2002
Location: glbTheWorld.Europe.UK
Posts: 1,201
Default

I suppose the next point, then is "Why actually use sliders?". You could base the position of the eye on the position of your mouse click when close to it!


Paul.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming Ideas and help fswinnie04 Game Programming 4 03-30-2004 03:57 AM
Client / Server ideas Alpha_7 Communications 2 09-09-2003 12:59 AM
Runtime Error 429 - Out of ideas. Jibaku General 2 06-11-2003 12:41 PM
out of ideas ericgamer47 Tech Discussions 10 12-20-2002 02:02 PM
XML Web Service.. Ideas.. Anis Tech Discussions 0 08-09-2002 07:37 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->