3

I have a component called Obstacle and I am using it as a component from my mainContentComponent using addAndMakeVisible and setBounds function. I wish to scroll this Obstacle component across my window, with respect to time. I am using a timer and setBounds function to do this.

I realize that using setBounds calls the paint function everytime, and that my component is created everytime the paint function is called. I would like to use the viewPort class and pass my component to a viewport object using the setViewedComponent class. I used the API, but not much has helped. Can someone point me to examples as to how to use the ViewPort, to scroll my juce::component across the window?

Many thanks.

Rithesh
  • 199
  • 7
  • Did my answer help you? If so, please mark it as accepted or state why the answer is not sufficient. – OMGtechy Oct 24 '15 at 12:01

1 Answers1

3

You are correct in thinking that you should use Viewport::setViewedComponent for this purpose.

For example:

myViewport.setViewedComponent (new MyComponent());

The viewport will handle the lifetime of the component for you.

You don't state exactly what problem you're having, but a common mistake is to forget to set the size of the viewed component. You can do this with Component::setSize inside your class to be viewed.

For example:

MyComponent::MyComponent()
{
    setSize (100, 100);
}

More information on Viewport can be found here.
More information on Component can be found here.

Ruurd Adema
  • 920
  • 7
  • 17
OMGtechy
  • 7,935
  • 8
  • 48
  • 83